-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathascii.py
More file actions
118 lines (83 loc) · 2.49 KB
/
Copy pathascii.py
File metadata and controls
118 lines (83 loc) · 2.49 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
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
108
109
110
111
112
113
114
115
116
117
from math import floor
from time import time
from PIL import Image, ImageDraw, ImageFont
import sys
if len(sys.argv) != 3:
sys.exit("Usage: python ascii.py INPUT_FILE OUTPUT_FILE")
input_file = sys.argv[1]
output_file = sys.argv[2]
ASCII_CHARS = {
0: "@",
1: "#",
2: "S",
3: "%",
4: "?",
5: "*",
6: "+",
7: ";",
8: ":",
9: ",",
10: ".",
11: " "
}
sub_part = len(ASCII_CHARS)/255
# normal value of scalar_factor is 0.3
# config for concentrated image
# scalar_factor = 0.6 # 0.6 <= scalar_factor <= 1
# font_width = 2
# font_height = 6
# config for light image
scalar_factor = 0.2 # decrease to less than or equal to 0.1 if size of image is large
font_width = 5 # you can change this value on your use(between 5 and 25)
font_height = 5 # you can change this value on your use (try to keep it same as font_width)
# for adding different fonts
# font = ImageFont.truetype("ARIAL.TTF", 15)
def import_image(input_file):
try:
im = Image.open(input_file)
return im
except Exception as error:
print("Provide proper name or extension of input file")
print(error)
sys.exit(1)
def greyify_and_resize(im):
im = im.convert('L')
width = im.width
height = im.height
return im.resize((int(width*scalar_factor), int(height*scalar_factor*(font_width/font_height))))
def create_new_image(im):
width = im.width
height = im.height
output_img = Image.new(mode='L', size=(width*font_width, height*font_height), color=(255))
return output_img
def pixel_to_ascii(rgb):
if rgb == 255:
return ASCII_CHARS[len(ASCII_CHARS)-1]
return ASCII_CHARS[floor(sub_part*rgb)]
def save_image(new_image):
try:
new_image.save(output_file)
except Exception as error:
print("Provide proper name or extension of Output file")
print(error)
sys.exit(1)
def main():
t1 = time()*1000
im = import_image(input_file)
im = greyify_and_resize(im)
new_image = create_new_image(im)
d = ImageDraw.Draw(new_image)
height = im.height
width = im.width
print(width, height)
for i in range(height):
for j in range(width):
rgb = im.getpixel((j,i))
char = pixel_to_ascii(rgb)
d.text((j*font_width, i*font_height), char, fill=(0))
save_image(new_image)
im.close()
t2 = time()*1000
print(f"Total time taken {(t2-t1)/1000}s")
if __name__ == '__main__':
main()