-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
44 lines (38 loc) · 1.33 KB
/
main.py
File metadata and controls
44 lines (38 loc) · 1.33 KB
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
import PIL
from PIL import Image
import os
import time
# 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):
width, height = image.size
ratio = height / width
new_height = int(new_width * ratio)
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()
characters = "".join([ASCII_CHARS[pixel // 25] for pixel in pixels])
return (characters)
def main(new_width=100):
#Path to folder containing pictures
path = "PATH"
for image_path in os.listdir(path):
image = PIL.Image.open(path + "\\" + image_path)
# convert image to ascii
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
# format
pixel_count = len(new_image_data)
ascii_image = "\n".join([new_image_data[index:(index + new_width)] for index in range(0, pixel_count, new_width)])
#print result
print(ascii_image)
##Delay between each image
time.sleep(1/10)
# run program
main()