-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathimage_ascii.py
56 lines (44 loc) · 1.46 KB
/
image_ascii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from PIL import Image
import sys
# ASCII characters used to build the output text
ASCII_CHARS = ['@', '#', 'S', '%', '?', '*', '+', ';', ':', ',', '.']
# Resize image according to a new width
def resize_image(image, new_width=100):
(old_width, old_height) = image.size
aspect_ratio = old_height/float(old_width)
new_height = int(aspect_ratio * new_width)
resized_image = image.resize((new_width, new_height))
return resized_image
# Convert each pixel to grayscale
def grayify(image):
grayscale_image = image.convert("L")
return grayscale_image
# Convert pixels to a string of ASCII characters
def pixels_to_ascii(image):
pixels = image.getdata()
ascii_str = ''
for pixel in pixels:
ascii_str += ASCII_CHARS[pixel//25]
return ascii_str
def main(image_path, new_width=100):
try:
image = Image.open(image_path)
except Exception as e:
print(e)
return
image = resize_image(image)
image = grayify(image)
ascii_str = pixels_to_ascii(image)
img_width = image.width
ascii_str_len = len(ascii_str)
ascii_img=""
# Split the string based on width of the image
for i in range(0, ascii_str_len, img_width):
ascii_img += ascii_str[i:i+img_width] + "\n"
# Print the result to the screen
print(ascii_img)
# Save the string to a file
with open("ascii_image.txt", "w") as f:
f.write(ascii_img)
# Run the program
main(sys.argv[1], new_width=100)