-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
45 lines (33 loc) · 1.11 KB
/
main.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
import PIL.Image
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import time
ASCII_CHARACTERS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
def change_image_size(image,new_width=100):
width,height=image.size
hw_ratio=height/width
new_height=int(new_width*hw_ratio)
changed_image_size=image.resize((new_width,new_height))
return(changed_image_size)
def turn_image_to_grey(image):
grayscale_image=image.convert("L")
return(grayscale_image)
def conversion_of_pixels_to_ascii(image):
pixels=image.getdata()
characters = "".join([ASCII_CHARACTERS[pixel//25] for pixel in pixels])
return(characters)
def main(new_width=100):
Tk().withdraw()
print("Please select your file")
time.sleep(1)
path=askopenfilename()
try:
image=PIL.Image.open(path)
except:
print(path,"is not a valid file")
data_new_image=conversion_of_pixels_to_ascii(turn_image_to_grey(change_image_size(image)))
ascii_image="\n".join(data_new_image[i:(i+new_width)] for i in range(0,len(data_new_image),new_width))
print(ascii_image)
with open("ascii_image_file.txt","w") as f:
f.write(ascii_image)
main()