-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpyascii.py
130 lines (116 loc) · 4.89 KB
/
pyascii.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
from PIL import Image, ImageFont, ImageDraw, ImageEnhance
# Version 0.3
class Pyascii:
def __init__(
self,
image_path,
save_path,
chars=None,
enh_color=None,
enh_brg=None,
font_path=None,
rgb_color=None,
optimize=False,
quality=100
):
self.optimize = optimize
self.quality = quality
self.rgb_color = rgb_color
self.enh_color = enh_color
self.enh_brg = enh_brg
self.image_path = image_path
self.save_path = save_path
self.bg_color = "black"
# (0, 0, 0)
# If no color is provided to bg_color PIL library will by
# default set the background color to black.
# If we set the new image mode to RGBA and bg_color is None
# the image will have no background
self.default_chars = list("_^:-=fg#%@") if chars == None else chars
# _.:-=fg#%@ -> First char list
# ^.:-=fg#%\\ -> Second char list
self.block_size = 6
self.font_size = 12
# Some good block size -> 6 }
# Some good font size -> 12 } Resolution 1024x768
# and with img.enhance_color(2.0)
# without brightness enhancement
self.font = "Anonymous_Pro.ttf" if font_path == None else font_path
self.char_brightness_list = self.char_brightness()
self.image = Image.open(self.image_path)
self.font = ImageFont.truetype(self.font, self.font_size)
self.rgb_image = self.image.convert("RGB")
self.final_image = Image.new(self.rgb_image.mode,
self.rgb_image.size, self.bg_color)
self.w, self.h = self.image.size
self.draw = ImageDraw.Draw(self.final_image)
def avg_brightness_pixel(self, r, g, b):
# Returns the average brightness value of a pixel
return (r + g + b) / 3
def default_convert(self):
# Default function to convert an image to a ascii image
self.print_info()
for i in range(0, self.w, self.block_size):
for j in range(0, self.h, self.block_size):
color = (r, g, b) = self.rgb_image.getpixel((i, j))
avg_brg = self.avg_brightness_pixel(r, g, b)
self.draw_text(self.default_chars[self.check_loop(avg_brg)],
self.draw, i, j, self.font, color)
if self.enh_color != None:
self.enhance_color(self.enh_color)
if self.enh_brg != None:
self.enhance_brightness(self.enh_brg)
def check_loop(self, value):
# We're doing a loop in range 10 (length of the ascii chars we use)
# and it returns what index of the ascii character we need to use
for i in range(len(self.default_chars)):
if value < self.char_brightness_list[i]:
return i
elif (value >= self.char_brightness_list[i-1] and
value < self.char_brightness_list[i]):
return i
def image_show(self):
# Show the ascii image
self.final_image.show()
def char_brightness(self):
# Will return a list of all the brightness values for each character
# in the charset
amount_levels = len(self.default_chars)
inner_list = []
for i in range(amount_levels):
level_brightness = (i + 1) * (255 / amount_levels)
inner_list.append(level_brightness)
return inner_list
def draw_text(self, char, draw, x, y, font, color):
# Draw the char into the new image we created (self.final_image)
# at x, y with the default font + default color
return draw.text((x, y), char, font=font, fill=color)
def enhance_color(self, value=1.0):
# Enhance the color of the final image
# Default value is 1.0
# - 0.0 for gray
# - 1.0 default
# - 2.0 enhanced :D
enhancer = ImageEnhance.Color(self.final_image)
self.final_image = enhancer.enhance(value)
def enhance_brightness(self, value=1.0):
# Enhance the brightness of the image
# Default value is 1.0
enhancer = ImageEnhance.Brightness(self.final_image)
self.final_image = enhancer.enhance(value)
def print_info(self):
# Print info about the original image, font, chars used, width + height
print(f"Size: {self.w}, {self.h}\nImage Path: {self.image_path}\nFont: {self.font.getname()}"
f"\nChars used: {self.default_chars}")
def image_save(self):
# Save the final image to the specified path
# Optimize=True -> Default=False
# quality -> Default=None
self.final_image.save(
self.save_path,
optimize=self.optimize,
quality=self.quality
)
def default_show(self):
# Show the default image
self.image.show()