Skip to content

Commit 772f2e3

Browse files
authored
Merge pull request HarshCasper#304 from AkM-2018/dev_asciify
Added ASCIIfy Image
2 parents 888e8ec + 725478b commit 772f2e3

File tree

5 files changed

+196
-86
lines changed

5 files changed

+196
-86
lines changed

Python/ASCIIfy_Image/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# ASCIIfy your image
2+
3+
This python script converts the input image to an ascii version of itself. This is achieved by giving different range of grayscale value to different special characters from the ASCII set. The whole image is then converted based on this.
4+
5+
## Setting up:
6+
7+
- Create a virtual environment and activate it
8+
9+
- Install the requirements
10+
11+
```sh
12+
$ pip install pillow
13+
```
14+
15+
## Running the script:
16+
17+
```sh
18+
$ python asciify.py [image_path] #without the brackets
19+
```
20+
21+
## Example running the script
22+
23+
```sh
24+
$ python asciify.py input.jpg
25+
```
26+
27+
![Input image](input.jpg)
28+
29+
### Will be converted to....
30+
<br>
31+
32+
![Converted image](image.png)

Python/ASCIIfy_Image/asciify.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import sys
2+
from PIL import Image, ImageDraw
3+
4+
'''
5+
A grayscale pixel value is converted into one of these special characters
6+
'''
7+
ASCII_SET = ['@','#','$','%','?','*','+',';',':',',','.']
8+
9+
'''
10+
method resize()
11+
- takes parameter image and new_width
12+
- resizes the image into new_width maintaining the aspect aspect_ratio
13+
- returns this resized image, new width and new height
14+
'''
15+
def resize(image, new_width=100):
16+
(old_width, old_height) = image.size
17+
aspect_ratio = old_height/old_width
18+
new_height = int(aspect_ratio*new_width)
19+
new_image = image.resize((new_width,new_height))
20+
return new_image, new_width, new_height
21+
22+
'''
23+
method convertToGray()
24+
- takes parameter image
25+
- returns converted grayscale image
26+
'''
27+
def convertToGray(image):
28+
return image.convert('L')
29+
30+
'''
31+
method PixelToAscii()
32+
- takes parameter image
33+
- returns a string with all image pixels converted to ascii special symbols from ASCII_SET
34+
'''
35+
def PixelToAscii(image, buckets=25):
36+
pixels = list(image.getdata())
37+
new_pixels = [ASCII_SET[pixel_value//buckets] for pixel_value in pixels]
38+
return ''.join(new_pixels)
39+
40+
'''
41+
method saveImage()
42+
- takes parameter string, new width and new height
43+
- writes the string in an image and saves the image as image.png
44+
'''
45+
def saveImage(ascii_str, new_width, new_height):
46+
image = Image.new(mode = "RGB", size = (new_width*11,new_height*11), color = "white")
47+
draw = ImageDraw.Draw(image)
48+
draw.multiline_text((0,0), ascii_str, fill=(0,0,0), align='center',spacing=0)
49+
image.save('image.png')
50+
51+
'''
52+
method asciify()
53+
- takes parameter image path
54+
- calls the required functions to asciify the image
55+
'''
56+
def asciify(img_path):
57+
try:
58+
image = Image.open(img_path)
59+
except Exception:
60+
print("Unable to find image in", img_path)
61+
return
62+
image, new_width, new_height = resize(image)
63+
gray_image = convertToGray(image)
64+
ascii_char_list = PixelToAscii(gray_image)
65+
66+
len_ascii_list = len(ascii_char_list)
67+
ascii_str = ''
68+
ascii_str = ''.join([ascii_str+i+i for i in ascii_char_list])
69+
70+
ascii_str = [ascii_str[index:index+2*new_width] for index in range(0, 2*len_ascii_list, 2*new_width)]
71+
ascii_str = "\n".join(ascii_str)
72+
73+
saveImage(ascii_str, new_width, new_height)
74+
75+
if __name__ == '__main__':
76+
if len(sys.argv)>1:
77+
img_path = str(" ".join(sys.argv[1:]))
78+
asciify(img_path)

Python/ASCIIfy_Image/image.png

59.8 KB
Loading

Python/ASCIIfy_Image/input.jpg

36.1 KB
Loading

0 commit comments

Comments
 (0)