-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii_render.py
73 lines (56 loc) · 1.96 KB
/
ascii_render.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
from PIL import Image
import numpy as np
import argparse
ASCII_VALUES = "@%#*+=-:. "
IMG_SIZE = (64, 64)
def convert_ascii(img_arr: list):
'''Converts a 1D image array into a 2D array of ASCII characters
Args:
img_arr (list): greyscale array of image
Returns:
ndarray: 2D ascii array of image
'''
for i in range(len(img_arr)):
char = int(img_arr[i] * ((len(ASCII_VALUES)-1)/255))
img_arr[i] = ASCII_VALUES[char]
return np.reshape(img_arr, IMG_SIZE)
def print_drawing(ascii_arr: np.ndarray, save=False):
'''Prints a 2D ASCII array into the console
Args:
ascii_arr (ndarray)
'''
frame = ''
for row in ascii_arr:
line = ''
for pixel in row:
line += pixel
frame += line + '\n'
if save:
with open('art.txt', 'w') as f:
f.write(frame)
else:
print(frame)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Render Images with Text')
parser.add_argument('file', metavar='f',
help='the location of the file')
parser.add_argument('-d', dest='dark_mode', action='store_true',
help='turn on dark mode rendering')
parser.add_argument('-s', dest='size', type=int, nargs=2,
help='size of the image')
parser.add_argument('-w', dest='download', action='store_true',
help='save ascii render to a text file')
args = parser.parse_args()
parser.set_defaults(dark_mode=False)
parser.set_defaults(download=False)
if args.size:
IMG_SIZE = tuple(args.size)
if args.dark_mode:
ASCII_VALUES = " .:-=+*#%@"
file = args.file
image = Image.open(file)
resized = image.resize(IMG_SIZE)
greyscale = resized.convert('L')
data = list(greyscale.getdata(0))
ascii_arr = convert_ascii(data)
print_drawing(ascii_arr, args.download)