-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (56 loc) · 1.83 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
#!/usr/bin/env python3
# pylint: disable=missing-docstring
import math
from PIL import Image
import os
def img_convert(ref, width, height):
# define shades and range
shades = [ '@', '#', ':', '!', '?']
s_range = 255 / len(shades)
# convert pixels in shades
pixels = []
for y in range(0, height):
line = []
for x in range(0, width):
point = ref.getpixel((x, y))
pixel = math.ceil(point[0] + point[1] + point[2] / 3)
shade = ''
if (pixel >= s_range * 4):
shade = shades[0]
elif (pixel >= s_range * 3):
shade = shades[1]
elif (pixel >= s_range * 2):
shade = shades[2]
elif (pixel >= s_range * 1):
shade = shades[3]
else:
shade = shades[4]
line.append(shade)
pixels.append(''.join(line) + '\n')
write_file(''.join(pixels))
# remove the comment below to see the result in the terminal
# print(''.join(pixels))
def write_file(text):
# open new file in append mode
if not does_folder_exist():
os.mkdir('assets')
f = open('assets/cnv_img.txt', 'w')
# write and close file
f.write(text)
f.close()
def does_folder_exist():
return os.path.isdir('assets')
def main():
# get img real path of user
img_path = input('(≧∀≦)ゞ - Tell to me what\'s the path to image: ')
try:
img = Image.open(img_path)
except FileNotFoundError:
print('(-_-) - Image not found. I\'m quitting...')
exit()
# resize img to 100px x 100px
img_ref = img.resize((100, 100))
# resize image and override the file
img_convert(img_ref, img_ref.width, img_ref.height)
print('(∩^o^)⊃━☆゜.* - It\'s over, the result are in cnv_img.txt')
main()