-
Notifications
You must be signed in to change notification settings - Fork 3
/
ascii.py
59 lines (49 loc) · 1.02 KB
/
ascii.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
from scipy.misc import imread, imsave, imresize
import numpy as np
import matplotlib.pyplot as plt
import re, sys
if len(sys.argv)!=2 or '.' not in sys.argv[1]:
print('Usage\n\t'+sys.argv[0]+' <image-file>')
exit(0)
WIDTH = 124
def get_ascii(p):
p = 255 - p
if p<=12:
return ' '
if p<=50:
return '.'
if p<=75:
return '*'
if p<=100:
return '='
if p<=125:
return '|'
if p<=150:
return 'J'
if p<=200:
return 'P'
return 'B'
def to_grayscale(img):
if len(img.shape) == 2:
return img
g = np.sum(img, axis=2);
g = g / 3
return g
img = imread(sys.argv[1])
h = len(img)
w = len(img[0])
ar = float(h)/w
img = imresize(img, (int(ar*WIDTH/1.75), WIDTH))
img = to_grayscale(img)
print(img)
print(img.shape)
filename = sys.argv[1]
filename = re.sub(r'[.*\.](.*)', '.txt', filename)
file = open(filename, 'w')
for i in range(len(img)):
ascii_str = ''
for j in range(len(img[0])):
ascii_str = ascii_str + get_ascii(img[i][j])
ascii_str = ascii_str + '\n'
file.write(ascii_str)
print("Ascii image saved to "+filename)