-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff
More file actions
131 lines (103 loc) · 3.2 KB
/
diff
File metadata and controls
131 lines (103 loc) · 3.2 KB
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
import scipy as sp
import numpy as np
from scipy import misc
from scipy import ndimage
import argparse
import matplotlib.pyplot as plt
import Image,ImageDraw,ImageFont
import random
import unicodedata
import cairo
parser = argparse.ArgumentParser()
parser.add_argument("fileName", help="display a square of a given number", type=str)
parser.add_argument("res", help="display a square of a given number", type=int)
args = parser.parse_args()
image = sp.misc.imread(args.fileName,flatten=True) #WHY DOES THIS WORK!!??
res = args.res
#image = misc.lena()
sx = ndimage.filters.sobel(image, axis=0)
sy = ndimage.filters.sobel(image, axis=1)
sob = np.hypot(sx, sy)
avg = np.average(sob)
vals = sob.shape
y = vals[0]
x = vals[1]
print("old shape", sob.shape)
if y%res != 0:
dy = res-(y%res)
else:
dy = 0
if x%res != 0:
dx = res-(x%res)
else:
dx = 0
print(dy, dx)
sob = np.pad(sob,((0,dy),(0,dx)),'constant', constant_values=(0))
print("new shape", sob.shape)
#sob[sob == ''] = 0
# Remember that these are going to be opposite
# ie, .shape gives (y,x)
print("width",x, " height", y)
# plt.imshow(sob, cmap='Greys_r')
# plt.show()
xs = (x/res)+1
ys = (y/res)+1
out = np.chararray((ys, xs), unicode=True)
print("shape of output ", out.shape)
render = ['#','|','-','','/','\\', u"\u2610",'(',
')','{','}','[',']','1','2','3','4','5','6','7','8','9','0','q',
'w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j',
'k','l',';',"'",'z','x','c','v','b','n','m',',','.','/','`','"',
'!','@','#','$','%','^','&','*','+','=','_',]
comp = []
font = ImageFont.truetype("OpenSans-Regular.ttf", int(res*1.2))
for target in render:
# data = np.zeros((12, 12, 4), dtype=np.uint8)
# surface = cairo.ImageSurface.create_for_data(data, cairo.FORMAT_ARGB32, 12, 12)
# ctx = cairo.Context(surface)
# ctx.set_source_rgb(0, 0, 0)
# ctx.rectangle(0, 0, 12, 12)
# ctx.fill()
# ctx.select_font_face("OpenSans-Regular.ttf")
# ctx.set_font_size(12)
# ctx.move_to(4,9)
# ctx.set_source_rgb(1, 1, 1)
# ctx.show_text(target)
# comp.append(data[:,:,0])
# plt.imshow(data[:,:,0], cmap='Greys_r')
# plt.show()
y1, x1 = font.getsize(target)
#print(x1,y1)
canvas = Image.new('L', (int(x1/1.5), y1), "black")
draw = ImageDraw.Draw(canvas)
# This is using <x,y> in contrast to everything else
draw.text((0, -int((res)/2)), target, font = font, fill = "white")
# plt.imshow(canvas, cmap='Greys_r')
# plt.show()
img_resized = canvas.resize((res,res), Image.ANTIALIAS)
comp.append(img_resized)
# plt.imshow(img_resized, cmap='Greys_r')
# plt.show()
def compare(arr):
"""Takes an input array and returns the ascii character that best represents it"""
mseMax = 999999999
use = ''
for i in range(0,len(render)):
err = np.sum((comp[i] - arr) ** 2)
if err < mseMax:
mseMax = err
use = render[i]
return use
for j in xrange(0,y,res):
for i in xrange(0,x,res):
array = sob[j:j+res, i:i+res]
out[(j/res),(i/res)] = compare(array)
canvas2 = Image.new('RGB', (x+100, y+100), (255, 255, 255))
draw = ImageDraw.Draw(canvas2)
for j in range(0,ys):
for i in range(0,xs):
a = unicode(out[j, i])
# This is using <x,y> in contrast to everything else
draw.text((i*res+10,j*res+5), a, font = font, fill = "#000000")
plt.imshow(canvas2)
plt.show()