-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASCII.py
47 lines (24 loc) · 1001 Bytes
/
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
from PIL import Image # important image processing
image_path = "sss.jpg" # set your image path
img = Image.open(image_path) # open your image
# resizeing the image
width, height = img.size
aspect_ratio = height/width
new_width = 120
new_height = aspect_ratio * new_width * 0.35
img = img.resize((new_width, int(new_height)))
# convert image to greyscale format
img = img.convert('L')
# getting pixels
pixels = img.getdata()
# replace each pixel with a character from array
chars = ["(",")","#","&","@","$","%","*","\\",":","/"]
new_pixels = [chars[pixel//25] for pixel in pixels]
new_pixels = ''.join(new_pixels)
# split string of chars into multiple strings of length equal to new width and create a list
new_pixels_count = len(new_pixels)
ascii_image = [new_pixels[index:index + new_width] for index in range(0, new_pixels_count, new_width)]
ascii_image = "\n".join(ascii_image)
# output on console
# note increase console length to see proper output
print(ascii_image)