-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·288 lines (264 loc) · 8.62 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
from argparse import ArgumentParser
from PIL import Image, ImageFilter
parser = ArgumentParser(description='Convert images to ASCII art')
parser.add_argument(
'--width',
'-w',
type=int,
default=100,
help='width of the output in characters')
parser.add_argument(
'--font-ratio',
'-r',
type=float,
default=0.5,
help='ratio of the width of the font to the height of the font')
parser.add_argument(
'--output', '-o', help='output file (defaults to filename.txt)')
parser.add_argument('image', help='input image file')
parser.add_argument(
'--pixel', '-p', help='flag for pixel art mode', action='store_true')
parser.add_argument(
'--invert',
'-i',
help='set foreground color to black and background color to white, only works in ASCII art mode',
action='store_true')
parser.add_argument(
'--high',
'-H',
help='use more gray levels for more detail',
action='store_true')
parser.add_argument(
'--stdout',
'-s',
help='output result to stdout, ignore --output',
action='store_true')
parser.add_argument(
'--strip-emptylines',
'-S',
help='strip useless newlines from beginning and end of the output',
action='store_true')
args = parser.parse_args()
if not args.stdout:
if args.output is None:
args.output = args.image + ".txt"
def foreList(fore):
return [
"\033[38;2;", str(fore[0]), ";", str(fore[1]), ";", str(fore[2]), "m"
]
def backList(back):
return [
"\033[48;2;", str(back[0]), ";", str(back[1]), ";", str(back[2]), "m"
]
def colorChar(rgba1, rgba2, b, f):
upPrint = False
downPrint = False
outputList = []
if rgba1[3] >= 100:
upPrint = True
if rgba2[3] >= 100:
downPrint = True
if (upPrint and downPrint):
up = [
int(rgba1[0] * rgba1[3] / 255), int(rgba1[1] * rgba1[3] / 255),
int(rgba1[2] * rgba1[3] / 255)
]
down = [
int(rgba2[0] * rgba2[3] / 255), int(rgba2[1] * rgba2[3] / 255),
int(rgba2[2] * rgba2[3] / 255)
]
if up == down:
if b is None:
b = up
outputList.extend(backList(b))
outputList.append(" ")
else:
if b is None:
if f is None:
b = up
f = down
outputList.extend(backList(b))
outputList.extend(foreList(f))
outputList.append("▄")
else:
if f == down:
b = up
outputList.extend(backList(b))
outputList.append("▄")
elif f == up:
b = down
outputList.extend(backList(b))
outputList.append("▀")
else:
b = up
outputList.extend(backList(b))
f = down
outputList.extend(foreList(f))
outputList.append("▄")
else:
if b == up:
if f is None:
f = down
outputList.extend(foreList(f))
outputList.append("▄")
else:
if f != down:
f = down
outputList.extend(foreList(f))
outputList.append("▄")
elif b == down:
if f is None:
f = up
outputList.extend(foreList(f))
outputList.append("▀")
else:
if f != up:
f = up
outputList.extend(foreList(f))
outputList.append("▀")
else:
if f is None:
b = up
outputList.extend(backList(b))
f = down
outputList.extend(foreList(f))
outputList.append("▄")
else:
if f == down:
b = up
outputList.extend(backList(b))
outputList.append("▄")
elif f == up:
b = down
outputList.extend(backList(b))
outputList.append("▀")
else:
b = up
outputList.extend(backList(b))
f = down
outputList.extend(foreList(f))
outputList.append("▄")
elif upPrint:
up = [
int(rgba1[0] * rgba1[3] / 255), int(rgba1[1] * rgba1[3] / 255),
int(rgba1[2] * rgba1[3] / 255)
]
if b is not None:
b = None
outputList.append("\033[49m")
if f != up:
f = up
outputList.extend(foreList(f))
outputList.append("▀")
elif downPrint:
down = [
int(rgba1[0] * rgba1[3] / 255), int(rgba1[1] * rgba1[3] / 255),
int(rgba1[2] * rgba1[3] / 255)
]
if b is not None:
b = None
outputList.append("\033[49m")
if f != down:
f = down
outputList.extend(foreList(f))
outputList.append("▄")
else:
if b is not None:
b = None
outputList.append("\033[49m")
else:
outputList.append(" ")
output = ''.join(outputList)
return output, b, f
def asciiChar(intensityTuple):
if intensityTuple[1] < 100:
return ' '
intensity = intensityTuple[0]
asciiString = ' .:-=+*#%@'
newIntensity = 9 - int(round(intensity * 10 / 255, 0))
return asciiString[newIntensity]
def highAsciiChar(intensityTuple):
if intensityTuple[1] < 100:
return ' '
intensity = intensityTuple[0]
asciiString = ' .\'`^",:;Il!i><~+_-?][}{1)(|\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$'
newIntensity = 69 - int(round(intensity * 70 / 255, 0))
return asciiString[newIntensity]
try:
img = Image.open(args.image)
except:
print("Unable to load image")
exit()
if args.pixel:
img = img.convert("RGBA")
args.font_ratio *= 2
else:
img = img.convert("LA")
basewidth = args.width
wpercent = float((basewidth / float(img.size[0])))
hsize = int((float(img.size[1]) * wpercent * args.font_ratio))
img = img.resize((basewidth, hsize), Image.LANCZOS)
im = img.load()
width, height = img.size
output = []
if args.pixel:
b = None
f = None
heightPairs = height // 2
remainder = height % 2
for i in range(heightPairs - 1):
for j in range(width):
newPixel, b, f = colorChar(im[j, i * 2], im[j, i * 2 + 1], b, f)
output.append(newPixel)
if b is not None:
b = None
output.append('\033[49m')
output.append('\n')
i = heightPairs - 1
for j in range(width):
newPixel, b, f = colorChar(im[j, i * 2], im[j, i * 2 + 1], b, f)
output.append(newPixel)
if remainder != 0:
if b is not None:
b = None
output.append('\033[49m')
i = height - 1
for j in range(width):
newPixel, b, f = colorChar(im[j, i], [0, 0, 0, 0], b, f)
output.append('\033[0m')
elif args.high:
for i in range(height):
if args.invert:
output.append("\033[30;47m")
for j in range(width):
output.append(highAsciiChar(im[j, i]))
if args.invert:
output.append("\033[0m")
output.append('\n')
del output[-1]
else:
for i in range(height):
if args.invert:
output.append("\033[30;47m")
for j in range(width):
output.append(asciiChar(im[j, i]))
if args.invert:
output.append("\033[0m")
output.append('\n')
del output[-1]
outputString = ''.join(output)
from re import sub
outputString = sub(r' +\n', '\n', outputString)
if args.strip_emptylines:
if args.pixel:
outputString = sub(r'^( *(.\[((0)|(49))m)?\n)+', '', outputString)
outputString = sub(r'\n+ +.\[0m\n?$', '\033[0m', outputString)
else:
outputString = sub(r'^( +\n)+', '', outputString)
outputString = sub(r'\n+ +\n?$', '', outputString)
if args.stdout:
print(outputString)
else:
outputFile = open(args.output, "w")
print(outputString, file=outputFile)