-
Notifications
You must be signed in to change notification settings - Fork 0
/
title.py
executable file
·450 lines (374 loc) · 16.6 KB
/
title.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/env python3
"""
Script: title.py
Date: 2020-05-04
Platform: MacOS/Windows/Linux
Description:
Print a title
"""
__author__ = 'thedzy'
__copyright__ = 'Copyright 2020, thedzy'
__license__ = 'GPL'
__version__ = '2.0'
__maintainer__ = 'thedzy'
__email__ = '[email protected]'
__status__ = 'Developer'
import os
import platform
import string
from typing import Optional
import numpy
from PIL import Image, ImageFont, ImageDraw, ImageChops, ImageOps
class Title:
def __init__(self, characters=string.ascii_letters + string.punctuation + string.digits,
font=None, font_height=25, font_variation=None,
font_for_brightness=None,
character_aspect=2.2, resolution=15, algorithm='abs',
invert=False):
# Character and render information
self.characters = str(characters) + ' '
self.character_info = {}
# Get the default font
system = platform.uname().system.lower()
if 'darwin' in system:
system_font = '/System/Library/Fonts/Menlo.ttc'
elif 'windows' in system:
system_font = 'C:/Windows/Fonts/consola.ttf'
elif 'linux' in system:
if os.path.exists('/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf'):
system_font = '/usr/share/fonts/truetype/ubuntu/UbuntuMono-R.ttf'
else:
system_font = '/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf'
if font is None:
self.display_font = system_font
else:
if os.path.exists(font):
self.display_font = font
else:
print('Path to {} cannot be accessed/found'.format(font))
self.display_font = system_font
# Dimension to create a sample character
self.sample_height, self.sample_width = 200, 200
# Set the default font size and style
self.font_height = font_height
self.font_variation = font_variation
# Load font to be used
self.font = ImageFont.FreeTypeFont(self.display_font, self.font_height, layout_engine=ImageFont.Layout.BASIC)
# If specified, set font variation, on error list variations
self.font = self.font.font_variant()
if self.font_variation:
try:
self.font.set_variation_by_name(self.font_variation)
except (ValueError, OSError):
try:
variations = font.get_variation_names()
print(f'Choose from the following variations:')
for variation in variations:
print(f'\t - {variation.decode()}')
except OSError:
print('Font does not support variations')
# Set the default font used for rendering
if font_for_brightness is None:
self.font_for_brightness = system_font
else:
if os.path.exists(font_for_brightness):
self.font = font
else:
print('Path to {} cannot be accessed/found'.format(font))
self.font = system_font
# Load font to be used for brightness
self.font_for_brightness = ImageFont.FreeTypeFont(self.font_for_brightness, self.sample_height,
layout_engine=ImageFont.Layout.BASIC)
# Set information to calculate rendering
self.character_aspect = character_aspect
self.resolution = resolution
self.algorithm = getattr(self, f'find_match_{algorithm}')
self.invert = invert
# Get window/terminal size
try:
self.window_width, _ = os.get_terminal_size()
except:
self.window_width = 180
self.upper_left, self.lower_right = self.__calculate_bounding_box()
self.__calculate_characters()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.__del__()
def __del__(self):
pass
def print(self, text, algorithm: Optional[str] = None, invert: bool = False) -> None:
"""
Print text
:param text: (str) Text to print
:param algorithm: (str) Algorithm to use
:param invert: (bool) Invert text
:return: None
"""
if algorithm is None:
algorithm = self.algorithm
else:
algorithm = getattr(self, f'find_match_{algorithm}')
# Get height
_, top, _, bottom = self.font.getbbox(self.characters)
text_height = int((bottom - top) * 1.4)
# Generate images for each lines
text_images = []
for line in text.split('\n'):
# Skip blank lines
if len(line.strip()) == 0:
continue
# Render text
text_image = Image.new('L', (self.window_width * 20, text_height), 0)
draw = ImageDraw.Draw(text_image)
draw.text((0, 0), line, font=self.font, fill=255, align='left')
# Crop empty space
image_black = Image.new(text_image.mode, text_image.size, 0)
diff = ImageChops.difference(text_image, image_black)
image_bounds = diff.getbbox()
# Crop out empty space on the width and height
text_image = text_image.crop((image_bounds[0], image_bounds[1], image_bounds[2], image_bounds[3]))
text_images.append(text_image)
# Find the largest image to scale evenly
image_width = 0
for text_image in text_images:
# Get image size
text_image_width, _ = text_image.width, text_image.height
image_width = text_image_width if text_image_width > image_width else image_width
# Find the scale for scaling down
resize_ratio = self.window_width / image_width if image_width > self.window_width else 1
# Print Characters
for text_image in text_images:
image_width, image_height = text_image.width, text_image.height
# Scale it smaller than 1 to 1 and by resolution
text_image = text_image.resize((int(image_width * resize_ratio * self.resolution),
int(image_height * resize_ratio * self.resolution / self.character_aspect)))
# Invert?
if invert is None:
if self.invert:
text_image = ImageOps.invert(text_image)
else:
if invert:
text_image = ImageOps.invert(text_image)
# Get final image size
image_width = text_image.width
image_height = text_image.height
# Write image to screen
line = 0
while line < image_height:
# For section/"column" in "row"
printed_line = ''
for index in range(image_width // self.resolution):
index = index * self.resolution
# Section of image (resolution * resolution)
pattern = numpy.zeros((self.resolution, self.resolution))
for col in range(self.resolution):
for row in range(self.resolution):
try:
pattern[row][col] = text_image.getpixel((index + col, line + row))
except IndexError:
pattern[row][col] = 0
# Find matching character for section append string
printed_line += algorithm(pattern, self.character_info, self.resolution)
print(printed_line)
# Jump to the next "row" of pixels in the image
line += self.resolution
def set_characters(self, characters: str) -> None:
"""
Set the default characters to use to render the text
:param characters: (string) Characters to render with
:return: (void)
"""
self.characters = str(characters) + ' '
self.__calculate_characters()
def set_character_aspect(self, character_aspect: float = 2.4) -> None:
"""
Set the default character aspect ratio
:param character_aspect: (float) Width to height ration, ex 2.4
:return: (void)
"""
self.character_aspect = float(character_aspect)
def set_font(self, font: str) -> None:
"""
Set the default font for rendering
:param font: (string) Path to font file
:return: (bool) Successfully set
"""
self.font = ImageFont.FreeTypeFont(font, self.font_height, layout_engine=ImageFont.Layout.BASIC)
def set_font_size(self, font_height: int = 20) -> None:
"""
Set the default font size
:param font_height: (int) Font size
:return: (void)
"""
self.font_height = int(font_height)
self.font = ImageFont.FreeTypeFont(self.display_font, self.font_height, layout_engine=ImageFont.Layout.BASIC)
def set_invert(self, invert: bool = False) -> None:
"""
Set whether the default is to invert or not
:param invert: (bool) To invert text
:return: (void)
"""
self.invert = bool(invert)
@staticmethod
def find_match_norm(pattern: numpy.ndarray, characters: dict, resolution: int) -> str:
"""
Compare a pattern and find the match in the character set using numpy linear algebra
:param pattern: 2d array of values
:param characters: (dict) Characters ans 2d arrays
:param resolution: (int) Array size
:return: (str) Character that best matches
"""
matched_character, matched_matrix = ' ', None
min_distance = numpy.inf
for character, matrix in characters.items():
distance = numpy.linalg.norm(pattern - matrix)
if distance < min_distance:
min_distance = distance
matched_character, matched_matrix = character, matrix
# Return best match
return matched_character
@staticmethod
def find_match_outline(pattern: numpy.ndarray, characters: dict, resolution: int) -> str:
"""
Compare a pattern and find the match in the character set using count of close matches
:param pattern: 2d array of values
:param characters: (dict) Characters and 2d arrays
:param resolution: (int) Array size
:return: (str) Character that best matches
"""
threshold = resolution / 4
best_match, matched_character = 0, ' '
for character, matrix in characters.items():
new_matrix = numpy.abs(pattern - matrix)
mask = (new_matrix < threshold) | (new_matrix > (256 - threshold))
distance = numpy.count_nonzero(mask)
if distance > best_match:
best_match = distance
matched_character, matched_matrix = character, matrix
# Return best match
return matched_character
@staticmethod
def find_match_low(pattern: numpy.ndarray, characters: dict, resolution: int) -> str:
"""
Compare a pattern and find the match in the character set using count of close low matches
:param pattern: 2d array of values
:param characters: (dict) Characters and 2d arrays
:param resolution: (int) Array size
:return:
"""
threshold = 40
best_match, matched_character = 0, ' '
for character, matrix in characters.items():
new_matrix = numpy.abs(pattern - matrix)
distance = sum(1 for x in new_matrix for y in x if y < threshold)
if distance > best_match:
best_match = distance
matched_character, matched_matrix = character, matrix
# Return best match
return matched_character
@staticmethod
def find_match_abs(pattern: numpy.ndarray, characters: dict, resolution: int) -> str:
"""
Compare a pattern and find the match in the character set using numpy sum(abs(matrix-matrix))
:param pattern: 2d array of values
:param characters: (dict) Characters ans 2d arrays
:param resolution: (int) Array size
:return:
"""
best_match = numpy.infty
matched_character = ' '
for character, matrix in characters.items():
simularity = numpy.sum(numpy.abs(pattern - matrix))
if simularity < best_match:
matched_character = character
best_match = simularity
# Return best match
return matched_character
@staticmethod
def find_match_abs2(pattern: numpy.ndarray, characters: dict, resolution: int) -> str:
"""
Compare a pattern and find the match in the character set using numpy filtering for count of values with less than 64 difference
:param pattern: 2d array of values
:param characters: (dict) Characters ans 2d arrays
:param resolution: (int) Array size
:return: (str) Character that best matches
"""
best_match = numpy.infty
for character, matrix in characters.items():
mask = (numpy.abs(pattern - matrix) > 64)
simularity = numpy.count_nonzero(mask)
if simularity < best_match:
matched_character = character
best_match = simularity
# Return best match
return matched_character
@staticmethod
def find_match_binary(pattern: numpy.ndarray, characters: dict, resolution: int) -> str:
"""
Compare a pattern and find the match in the character set using numpy binary matching
:param pattern: 2d array of values
:param characters: (dict) Characters ans 2d arrays
:param resolution: (int) Array size
:return: (str) Character that best matches
"""
best_match = numpy.infty
for character, matrix in characters.items():
new_pattern = numpy.interp(pattern, (0.0, 256.0), (0, 2)).astype(int)
new_matrix = numpy.interp(matrix, (0.0, 256.0), (0, 2)).astype(int)
mask = (numpy.abs(new_pattern - new_matrix) == 1)
simularity = numpy.count_nonzero(mask)
if simularity < best_match:
matched_character = character
best_match = simularity
# Return best match
return matched_character
def __calculate_bounding_box(self) -> tuple:
"""
Calculate the bounding box for the rending font
:return: (tuple)(tuple) Upper, lower coordinates
"""
# Render a sample character to get crop dimensions
character_image = Image.new('L', (self.sample_width * 2, self.sample_height * 2), 0)
draw = ImageDraw.Draw(character_image)
# Draw each character over each other
for index, char in enumerate(self.characters):
# Draw the character in the center of the image
draw.text((0, 0), char, font=self.font_for_brightness, fill=256)
# Get the bounding box of the characters
bounding_box = character_image.getbbox()
upper_left = bounding_box[0:2]
lower_right = bounding_box[2:4]
return upper_left, lower_right
def __calculate_characters(self) -> None:
"""
Calculate the array information for each character for mapping
:return: None
"""
self.character_info = {}
# Get brightness of a character in matrix
for supplied_char in self.characters:
self.character_info[supplied_char] = self.__image_chopper(supplied_char, self.upper_left,
self.lower_right)
def __image_chopper(self, character: str, top_left: numpy.ndarray,
bottom_right: numpy.ndarray) -> numpy.ndarray:
"""
Chop up image and return 2d array of values
:param character: Character to slice
:param top_left: (tuple) Coordinates
:param bottom_right:(tuple) Coordinates
:return: (numpy.ndarray) Matrix of brightness values
"""
# Get image size
width, height = bottom_right
# Create image of character
character_image = Image.new('L', (width, height), 0)
draw = ImageDraw.Draw(character_image)
draw.text((0, -top_left[1]), character, font=self.font_for_brightness, fill=255, align='left')
# Crop to bounds
character_image = character_image.crop((0, 0, bottom_right[0], bottom_right[1]))
# Resize to resolution and convert to greyscale
resized_image = character_image.resize((self.resolution, self.resolution))
resized_image = resized_image.convert('L')
# Return matrix of brightness values
return numpy.asarray(resized_image)