-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
107 lines (85 loc) · 2.94 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from PIL import Image, UnidentifiedImageError
from pathlib import Path
import shutil
def main():
def get_file():
input_path = Path('Input')
for p in input_path.iterdir():
return p
def create_output_file(filename):
filename = str(filename.stem) + '.txt'
output_path = Path('Output') / filename
return output_path
def get_output_file_path():
output_path = Path('Output')
return output_path
def map_to_ascii(color):
if color >= 240:
return "*"
elif 225 <= color < 240:
return "^"
elif 210 <= color < 225:
return "o"
elif 180 <= color < 195:
return "i"
elif 165 <= color < 180:
return "}"
elif 150 <= color < 165:
return "{"
elif 135 <= color < 150:
return "+"
elif 120 <= color < 135:
return "="
elif 105 <= color < 120:
return "G"
elif 90 <= color < 105:
return "E"
elif 75 <= color < 90:
return "M"
elif 60 <= color < 75:
return "W"
elif 45 <= color < 60:
return "$"
elif 30 <= color < 45:
return "&"
elif 15 <= color < 30:
return "@"
else:
return "#"
def dimensions(img):
img_width = int(img.size[0])
img_length = int(img.size[1] * 0.4) # 0.4 is to scale image
scale_factor = (max((img_width, img_length)) / 250)
img_width = int(img_width / scale_factor)
img_length = int(img_length / scale_factor)
return img_width, img_length
def resize_img(img):
img_dimensions = dimensions(img)
img_resized = img.resize((img_dimensions[0], img_dimensions[1]))
img_resized = img_resized.convert("L")
return img_resized
try:
with open(create_output_file(get_file()), 'w') as f:
with Image.open(get_file()) as img:
img_dimensions = dimensions(img)
new_img = resize_img(img)
px = new_img.load()
x=0
y=0
while y < img_dimensions[1]:
while x < img_dimensions[0]:
f.write(map_to_ascii(px[x,y]))
x = x + 1
y = y + 1
x = 0
f.write("\n")
shutil.move(str(get_file()), str(get_output_file_path()))
print("Finished!")
except AttributeError as e:
print("There is no image in the Input Directory!")
except FileNotFoundError as e:
print("You are missing the Input or Output Directory !")
except UnidentifiedImageError as e:
print("This program requires image files!")
if __name__ == '__main__':
main()