Skip to content

Commit

Permalink
stable 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
utnaf committed Apr 6, 2018
1 parent 296a2de commit 4240677
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## ASCII Stuff
## ASCIImage

This is an exercise I made to lear some Python.

Expand All @@ -11,7 +11,7 @@ It takes an image and prints on terminal it's ASCII color art representation.
You need [Pillow](https://github.com/python-pillow/Pillow) to run this script, so please make sure to have it installed and then run

```
python ascii.py test.jpg
python asciimage.py -f test.jpg
```

Where test.jpg is this one
Expand All @@ -22,6 +22,21 @@ Should return somethin like

![result in terminal](https://github.com/utnaf/ascii-stuff/raw/master/result.png)

## Other options

### Use an url as input

```
python asciimage.py -u https://bit.ly/2Iys8VP
```

### Output in grayscale

```
python asciimage.py -f test.jpg -g
```


### Various
Feel free to contribute, comment, suggest, cry, smile, or whatever you do when you see something awesome :)

Expand Down
27 changes: 19 additions & 8 deletions ascii.py → asciimage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import time
import sys
import os
import io
import urllib.request
import argparse
from src import *
from PIL import Image
from PIL import ImageStat
from pprint import pprint
from math import ceil


def main(image):
def main(image, is_grayscale):
count = 0
screen = Screen()

Expand Down Expand Up @@ -40,7 +43,7 @@ def main(image):

val = ((square_stats.mean[0] +
square_stats.mean[1]) / 2) % Char.MAX_LEN
sys.stdout.write(str(Char(int(val), rgb[0])))
sys.stdout.write(str(Char(int(val), rgb[0], is_grayscale)))

if x < range_x:
sys.stdout.write('\n')
Expand All @@ -63,25 +66,33 @@ def get_medium_color(image):
blue_amount += b

return rgb2short(
format(int(red_amount/count), '2x') +
format(int(red_amount/count), '2x') +
format(int(green_amount/count), '2x') +
format(int(blue_amount/count), '2x')
)


def get_image():
infile = sys.argv[1]

def get_image(file, url):
infile = file
if infile == None:
exit('Error')
with urllib.request.urlopen(url) as readed_url:
infile = io.BytesIO(readed_url.read())

fileobject = Image.open(infile).convert('RGB')

return fileobject


parser = argparse.ArgumentParser(description="Convert an image into ASCII art")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("-f", help="Local file path")
group.add_argument("-u", help="Remote url")

parser.add_argument("-g", help="Show the image in grayscale", action='store_true')
args = parser.parse_args()

try:
main(get_image())
main(get_image(args.f, args.u), args.g)
input()
except KeyboardInterrupt:
try:
Expand Down
Binary file modified result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 6 additions & 3 deletions src/char.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ class Char:
GREYSCALE_CHARS = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1\{\}[]?-_+~<>i!lI;:,\"^`'. "
MAX_LEN = len(GREYSCALE_CHARS)

def __init__(self, level, color):
def __init__(self, level, color, is_greyscale):
self._level = level
self._color = color
self._is_greyscale = is_greyscale

def __repr__(self):

return '\x1b[38;5;' + self._color + 'm' + self.GREYSCALE_CHARS[self._level]
if self._is_greyscale:
return self.GREYSCALE_CHARS[self._level]
else:
return '\x1b[38;5;' + self._color + 'm' + self.GREYSCALE_CHARS[self._level]

0 comments on commit 4240677

Please sign in to comment.