-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathascii_image.py
executable file
·185 lines (155 loc) · 6.06 KB
/
ascii_image.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
#! /usr/bin/env python3
import argparse
from PIL import Image
from termcolor import *
import colorama
import requests
from io import BytesIO
import json
import random
import string
import platform
colorama.init()
CHARS = ['.', ',', ':', ';', '+', '*', '?', '%', 'S', '#', '@']
PALETTE = (
12, 12, 12, # Gray
197, 15, 31, # Red
19, 161, 14, # Green
193, 156, 0, # Yellow
0, 55, 218, # Blue
136, 23, 152, # Magenta
58, 150, 221, # Cyan
204, 204, 204 # White
) + (0, 0, 0) * 248 # Filling the rest of the palette with black
PALETTE_COLORS = ['grey', 'red', 'green', 'yellow',
'blue', 'magenta', 'cyan', 'white', 'grey']
PALETTE_IMAGE = Image.new('P', (1, 1), 0)
PALETTE_IMAGE.putpalette(PALETTE)
def image_resize(image, width=None, height=None):
(old_width, old_height) = image.size
# Chars are drawn at a 2:1 height:width ratio in the terminal
old_height = old_height // 2
if width is None and height is None:
return image
if width is None:
aspect_ratio = height / float(old_height)
dim = (int(old_width * aspect_ratio), height)
else:
aspect_ratio = width / float(old_width)
dim = (width, int(old_height * aspect_ratio))
resized = image.resize(dim)
return resized
def image_to_ascii_greyscale(image, resolution, char_list):
image = image.convert('L')
greyscale_values = list(image.getdata())
ascii_pixels = ''.join(char_list[i // 25] for i in greyscale_values)
ascii_image = [ascii_pixels[i:i + resolution]
for i in range(0, len(ascii_pixels), resolution)]
return ascii_image
def image_to_ascii_color(image):
image_dithered = image.convert('RGB').quantize(palette=PALETTE_IMAGE)
image_values = list(image_dithered.getdata())
color_values = [PALETTE_COLORS[i] for i in image_values]
return color_values
# https://stackoverflow.com/a/26665998
def rgb_to_ansi_escape(r, g, b):
if r == g and g == b:
if r < 8:
return 16
if r > 248:
return 231
return round(((r - 8) / 247) * 24) + 232
ansi = 16 + (36 * round(r / 255 * 5)) + \
(6 * round(g / 255 * 5)) + round(b / 255 * 5)
return ansi
def supports_256_color():
if platform.system() in ("Linux", "Darwin"):
import curses
curses.setupterm()
if curses.tigetnum("colors") >= 256:
return True
return False
def print_image(img, resolution, greyscale=False, bg_color=False, fg_color=True):
if greyscale:
bg_color = False
fg_color = False
original_aspect_ratio = img.size[0] / img.size[1]
img = image_resize(img, width=resolution)
ascii_list = image_to_ascii_greyscale(img, resolution, CHARS)
ascii_pixels = '\n'.join(ascii_list)
_256_color = supports_256_color()
if _256_color:
color_values = list(img.getdata())
else:
color_values = image_to_ascii_color(img)
idx = 0 # Can't use enumerate because newline chars need to be ingnored
for i in ascii_pixels:
if i != '\n':
if _256_color:
color = rgb_to_ansi_escape(*color_values[idx])
print(
f"\033[48;5;{color if bg_color else 0};38;5;{color if fg_color else 256}m{i}\033[0;00m", end='')
idx += 1
else:
cprint(i, color=(color_values[idx] if fg_color else 'white'),
on_color=(
f'on_{color_values[idx]}' if bg_color else 'on_grey'),
end='')
idx += 1
else:
print(i, end='')
print('')
def get_image_from_url(url):
r = requests.get(args.url)
r.raise_for_status()
img = Image.open(BytesIO(r.content)).convert('RGB')
return img
def export_json(filepath, img, resolution):
original_aspect_ratio = img.size[0] / img.size[1]
resized = image_resize(img, width=resolution)
width, height = resized.size
ascii_list = image_to_ascii_greyscale(resized, resolution, CHARS)
ascii_pixels = '\n'.join(ascii_list)
values_dict = {
'width': width,
'height': height,
'aspect': original_aspect_ratio,
'image': ascii_pixels
}
rand_string = ''.join(random.choices(
string.ascii_uppercase + string.ascii_lowercase + string.digits, k=8))
with open(f'{filepath}/ascii_image_{rand_string}.json', 'w+') as outfile:
json.dump(values_dict, outfile)
print(f'{filepath}/ascii_image_{rand_string}.json exported.')
def main():
parser = argparse.ArgumentParser()
mods = parser.add_mutually_exclusive_group()
inputs = parser.add_mutually_exclusive_group()
parser.add_argument('-r', '--resolution', type=int, default=100,
help='Width to resize the image to, in pixels. Higher value means more detail. Default=100')
parser.add_argument('-j', '--json', type=str,
help='Specify filepath for JSON output. To be used for embedding in webpages.')
mods.add_argument('-c', '--color', action='store_true',
help='Print the ascii charecters to the console in color')
parser.add_argument('-b', '--background', action='store_true',
help='Print the ascii charecters to the console with colored backgrounds')
inputs.add_argument('-f', '--file', type=str,
help='Specify a file to turn into ASCII')
inputs.add_argument('-u', '--url', type=str,
help='Specify an image URL to turn into ASCII')
args = parser.parse_args()
if args.url:
img = get_image_from_url(args.url)
elif args.file:
img = Image.open(args.file)
else:
raise ValueError("Must provide image input")
if args.json:
export_json(args.json, img, args.resolution)
else:
foreground = True if args.color else False
background = True if args.background else False
greyscale = False if foreground or background else True
print_image(img, args.resolution, greyscale, background, foreground)
if __name__ == '__main__':
main()