-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathasciiart.py
79 lines (70 loc) · 2.08 KB
/
asciiart.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
from PIL import Image, ImageEnhance
import random
import os
_greyscale = [' ',
'`',
'.\'',
'-',
'^,',
'\/:"',
'~!_<>',
';',
'=',
'i',
'j',
'l17{}',
'tv[]',
'crI',
'fuJ2',
'oxLO3%',
'*',
'V',
'Y4569',
'ehNTZ0&',
'bdkwAFP',
'DX8$',
'pqGU',
'gKQS',
'BE',
'mRW#',
'H',
'M@'][::-1]
def _proportion(min1, max1, min2, max2, val):
diff1 = max1-min1
diff2 = max2-min2
val = float(val)
return ((val-min1)/diff1)*diff2 + min2
def get_ascii_art(imagePath, scale):
if not isinstance(imagePath, str) or not os.path.isfile(imagePath):
raise ValueError("Image path must be valid file path")
if not isinstance(scale, int) or scale < 0 or scale > 100:
raise ValueError("Scale must be an integer value between 0 and 100")
#Open the image file
image = Image.open(imagePath)
#Convert to greyscale
image = image.convert("L")
#Scale the image
image = image.resize((int(image.width*scale/100 * 1.5), int(image.height*scale/100)))
#Object to enhance contrast
contrast = ImageEnhance.Contrast(image)
#Enhance contrast
image = contrast.enhance(2)
#The resulting ASCII Art
out = ""
#Horizontal position of the current pixel
pos = 0
#Cycle through all pixel values
pixels = list(image.getdata())
#pixels = [x[2] for x in pixels]
for x in pixels:
#Add the correct character to the output string
out += random.choice(_greyscale[int(_proportion(0, 255, 0, len(_greyscale) - 1, x))])
pos += 1
if pos >= image.width:
out += "\n"
pos = 0
return out
if __name__ == "__main__":
with open("output.txt", "w") as f:
f.write(get_ascii_art("image.png", 50))
print("ASCII Art succesfully created")