-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
148 lines (121 loc) · 5.47 KB
/
convert.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from PIL import Image
import time as t
import glob, random, curses
def bruteAnim(sentence, word):
letters = ['a','c','d','f','g','i','k','m','o','q','s','u','w','y']
write = sentence+''
for a in range(0, len(word)):
for b in range(0,5):
print(write+letters[random.randint(0,len(letters)-1)], end='\r')
t.sleep(0.05)
write = write + word[a]
print(sentence+word)
# black, red, green, orange, blue, purple, cyan, lightgrey
# darkgrey, lightred, lightgreen, yellow, lightblue, pink, lightcyan
colorList = [[(28, 28, 28), '\033[30m'], [(224, 52, 52), '\033[31m'], [(42, 209, 75), '\033[32m'],
[(219, 141, 46), '\033[33m'], [(53, 48, 217), '\033[34m'], [(156, 43, 227), '\033[35m'],
[(53, 214, 219), '\033[36m'], [(171, 171, 171), '\033[90m'], [(99, 99, 99), '\033[90m'],
[(255, 163, 163), '\033[91m'], [(130, 255, 145), '\033[92m'], [(255, 251, 5), '\033[93m'],
[(150, 227, 255), '\033[94m'], [(255, 140, 230), '\033[95m'], [(204, 249, 255), '\033[96m']]
def rgbToAnsi(r, g, b):
""" convert rgb to ansi (closest of 15 default colours) """
# assume minDifference is the first pixel
ansiIndex = 0
minDifference = abs(colorList[0][0][0] - r)
minDifference += abs(colorList[0][0][1] - g)
minDifference += abs(colorList[0][0][2] - b)
for i in range(1, len(colorList)):
thisDifference = abs(colorList[i][0][0] - r)
thisDifference += abs(colorList[i][0][1] - g)
thisDifference += abs(colorList[i][0][2] - b)
if(thisDifference < minDifference):
minDifference = thisDifference
ansiIndex = i
return colorList[ansiIndex][1]
class Picture():
"""
Used for converting an jpg or png image to a text image.
Attributes:
filename (string): The location of the jpg/png file
"""
def __init__(self, filename, colourChoice):
self.colourChoice = colourChoice
self.ref = Image.open(filename)
self.rgb = self.ref.convert("RGB")
self.wXh = self.ref.size
def genAscii(self):
""" The method to print the ASCII image. """
darkToBright = ['#', '$', '&', '%', 'x', 'v', '=', '+', '*', '~', '`']
imgSize = 160 # max number of columns/rows
if(self.wXh[0] >= self.wXh[1]):
# landscape
xStep = imgSize
yStep = int(self.wXh[1]*imgSize/self.wXh[0])
else:
# portrait
xStep = int(self.wXh[0]*imgSize/self.wXh[1])
yStep = imgSize
incrementX = self.wXh[0] // xStep
incrementY = self.wXh[1] // (yStep//2) # divide by 2 because of terminal line height
for row in range(0, self.wXh[1], incrementY):
for col in range(0, self.wXh[0], incrementX):
(r, g, b) = self.rgb.getpixel((col, row))
totalBrightness = r + g + b # 0-765
# ascii color light gray
if(self.colourChoice == 1): # grayscale
print(darkToBright[totalBrightness//70], end='')
else:
print('\033[07m', end='') # reverse color
if(self.colourChoice == 2): # 4-bit
print(f"\033[47m{rgbToAnsi(r, g, b)+darkToBright[totalBrightness//70]}\033[0m", end='')
elif(self.colourChoice == 3): # true colour
print(f"\x1b[38;2;{r};{g};{b}m{darkToBright[totalBrightness//70]}\x1b[0m", end='')
print()
def main():
# supported file extensions
exts = ["jpg", "jpeg", "png", "gif", "bmp", "eps", "gif", "icns", "ico",
"im", "msp", "pcx", "ppm", "sgi", "tiff"]
possibleImages = []
imageNames = []
for ext in exts:
possibleImages += glob.glob(f"imgs/*.{ext}")
possibleImages += glob.glob(f"imgs/*.{ext.upper()}")
for img in possibleImages:
imageNames.append(img.split('imgs/')[1].split('.')[0])
print("Hello, type the associated number to make an ASCII image of one of the following images.")
c = 1
for img in imageNames:
print(f"({c}) {img} image")
c += 1
print("If you don't see your image listed, drop it into the ./imgs/ folder")
userNum = ""
while(not (userNum.isdigit() and 1 <= int(userNum) <= len(possibleImages))):
if(userNum.isdigit()):
if(not(1 <= int(userNum) <= len(possibleImages))):
print(f"Make sure the number is between or equal to 1 and {len(possibleImages)}")
userNum = input("Enter the number: ")
userNum = int(userNum)
print("Type the associated number to choose what type of colour to use.")
colourChoices = ["Grayscale", "4-Bit Colour", "True Colour"]
c = 1
for type in colourChoices:
print(f"({c}) {type}")
c += 1
colorChoice = ""
while(not (colorChoice.isdigit() and 1 <= int(colorChoice) <= 3)):
if(colorChoice.isdigit()):
if(not(1 <= int(colorChoice) <= 3)):
print("Make sure the number is between or equal to 1 and 3")
colorChoice = input("Enter the number: ")
colorChoice = int(colorChoice)
bruteAnim("Generating your ", "ASCII Image");
print()
t0 = t.time()
selectedImage = Picture(possibleImages[userNum-1], colorChoice)
selectedImage.genAscii()
print('\033[0m') # reset colors
timeElapsed = round(t.time() - t0, 2)
print(f"The image was generated in {timeElapsed} seconds.")
print("It might look like a bunch of random text now\nbut zoom out to see the picture!")
if __name__ == '__main__':
main()