-
Notifications
You must be signed in to change notification settings - Fork 0
/
title_creator_v1.py
executable file
·192 lines (157 loc) · 6.8 KB
/
title_creator_v1.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
#!/usr/bin/env python3
"""
Script: title_creator.py
Date: 2020-05-04
Platform: MacOS/Windows/Linux
Description:
Convert an text to title
"""
__author__ = 'thedzy'
__copyright__ = 'Copyright 2020, thedzy'
__license__ = 'GPL'
__version__ = '1.0'
__maintainer__ = 'thedzy'
__email__ = '[email protected]'
__status__ = 'Developer'
import argparse
import os
import platform
from PIL import Image, ImageFont, ImageDraw, ImageStat
def main():
# Load font to be used
font = ImageFont.truetype(options.font_brightness.name, 18)
options.font.close()
# Create shader
if options.characters is not None:
# Filter out duplicates
characters = list(set(options.characters))
characters.append(' ')
# Get average brightness of a character
character_values = {}
for character in characters:
character_image = Image.new('L', (10, 20), 0)
draw = ImageDraw.Draw(character_image)
draw.text((0, 0), character, font=font, fill=255, align='center')
image_mean = ImageStat.Stat(character_image).mean[0]
character_values[character] = image_mean
# Sort the values into an array
shader = []
while len(character_values) > 0:
key = min(character_values, key=character_values.get)
shader.append(key)
del character_values[key]
else:
shader = list(options.shader)
shader_length = len(shader) - 1
# Invert if black on white
if options.black_on_white:
shader = shader[::-1]
# Get dimension but skip entirely if using custom settings
window_width, window_height = os.get_terminal_size()
# Load image
font = ImageFont.truetype(options.font.name, options.font_height)
options.font.close()
image = Image.new('L', (window_width, int(options.font_height * 1.1)), 0)
draw = ImageDraw.Draw(image)
draw.text((0, 0), options.text, font=font, fill=255, align='center')
# Calculate new sizes
new_height = int(options.font_height / options.aspect)
new_width = window_width
# Resize image
image = image.resize((new_width, new_height))
# Get pixel data in RGB
image_data = image.getdata()
# Break image into a matrix of perceived values
image_matrix = []
for x in range(new_width):
row = []
for y in range(new_height):
image_pixel = image_data.getpixel((x, y))
image_contrast = int((image_pixel/255) * shader_length)
row.append(image_contrast)
image_matrix.append(row)
# Write data out
if options.border is not None:
print(options.border[0] * window_width)
for y in range(new_height):
for x in range(new_width):
print(shader[image_matrix[x][y]], end='')
print('')
if options.border is not None:
print(options.border[0] * window_width)
if __name__ == '__main__':
def parser_formatter(format_class, **kwargs):
"""
Use a raw parser to use line breaks, etc
:param format_class: (class) formatting class
:param kwargs: (dict) kwargs for class
:return: (class) formatting class
"""
try:
return lambda prog: format_class(prog, **kwargs)
except TypeError:
return format_class
# 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'
else:
system_font = None
parser = argparse.ArgumentParser(description='Create a title',
formatter_class=parser_formatter(
argparse.RawTextHelpFormatter,
indent_increment=4, max_help_position=12, width=160))
# Image file to read in
parser.add_argument('-t', '--text',
action='store', dest='text', default=None,
help='Text to display\n'
'Required',
required=True)
# Text of character to render from
parser.add_argument('-s', '--shader', type=str,
action='store', dest='shader',
default=' .\'`^",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$',
help='Light to dark characters to use\n'
'Default: %(default)s')
# Text of character to render from
parser.add_argument('-c', '--chars', type=str,
action='store', dest='characters', default=None,
help='Specify characters that the system will determine the order of\n'
'Overrides -s/--shader')
parser.add_argument('-b', '--border', type=str,
action='store', dest='border', default=None,
help='Border character')
# Font information
parser.add_argument('-fb', '--font-brightness', type=argparse.FileType('r'),
action='store', dest='font_brightness', default=system_font,
help='Font used to determine brightness when specifying characters\n'
'This should match or be a close approximation of your terminal/console font\n'
'Default: %(default)s')
parser.add_argument('-f', '--font', type=argparse.FileType('r'),
action='store', dest='font', default=system_font,
help='Font used to write in\n'
'Default: %(default)s')
parser.add_argument('-fh', '--font-height', type=int,
action='store', dest='font_height', default=25,
help='Font height\n'
'Default: %(default)s')
# Character aspect ratio
parser.add_argument('-a', '--char-aspect', type=int,
action='store', dest='aspect', default=2.4,
help='Aspect ratio of the terminal/console character, ADVANCED'
'Default: %(default)s\n'
'Adjust if aspect ration of the image feels off. Height / width of a character block')
# Black/white
parser.add_argument('-i', '--invert',
action='store_true', dest='black_on_white', default=False,
help='Invert black/white\n'
'Default: %(default)s')
options = parser.parse_args()
main()