-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.py
248 lines (210 loc) · 9.29 KB
/
draw.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import cv2
import numpy as np
import svgwrite
import os.path
import vg
import math
import base64
from random import randint
def readImageFromFileName(fn):
# cv2.imread does not throw an error if the input is bad
# Check if the path is at least valid
if not os.path.isfile(fn):
raise IOError('Input file not found: \'' + fn + '\'')
i = {}
i['img'] = cv2.imread(fn) # this honestly creates a hassle, but makes
i['h'], i['w'], _ = i['img'].shape # heighth and width easier to access
return i
def readImageFromFile(f):
file_bytes = np.asarray(bytearray(f.read()), dtype=np.uint8)
i = {}
i['img'] = cv2.imdecode(file_bytes, cv2.IMREAD_UNCHANGED)
i['h'], i['w'], _ = i['img'].shape
return i
def cleanOutname(on):
# Setup output
base = os.path.splitext(on)[0]
out = {}
out['im'] = base + vg.EXT_IMG
out['svg'] = base + '.svg'
return out
def gbr2gray(mean):
# Luminosity weighted grayscale conversion
mean = [m/float(255) for m in mean]
Y_lin = 0.2126*mean[2] + 0.7152*mean[1] + 0.0722*mean[0]
Y_lin = int(Y_lin*255)
return svgwrite.rgb(Y_lin, Y_lin, Y_lin)
def getTriangles(gray):
# Get corners and create list of Delaunay triangles
edges = cv2.Canny(gray, vg.INPUTS['EDGE_LOW']['val'], vg.INPUTS['EDGE_HIGH']['val'])
corners = np.int0(cv2.goodFeaturesToTrack(edges,vg.INPUTS['CORNERS_NUM']['val'],vg.INPUTS['CORNERS_LOW']['val'],vg.INPUTS['CORNERS_HIGH']['val']))
subdiv = cv2.Subdiv2D((0,0,gray.shape[1],gray.shape[0]))
for i in corners:
x, y = i.ravel()
subdiv.insert((x,y))
return subdiv.getTriangleList()
def edges(img, outname='edges', svgOutput=False, jpgOutput=False, b64Output=False):
# Only draw the edges of objects, all in grayscale
outname = cleanOutname(outname)
# Find edges and then make into list of contours
gray = cv2.cvtColor(img['img'], cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, vg.INPUTS['EDGE_HIGH']['val'], vg.INPUTS['EDGE_LOW']['val'])
thresh = cv2.adaptiveThreshold(edges,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY, vg.INPUTS['THRESH_BLOCK']['val'], vg.INPUTS['THRESH_C']['val'])
out, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
out = cv2.bitwise_not(out)
if jpgOutput:
cv2.imwrite(outname['im'], out)
# Draw the contours onto an SVG
if svgOutput:
dwg = svgwrite.Drawing(outname['svg'], size=(img['w'],img['h']))
for line in contours:
pts = [(int(p[0,0]),int(p[0,1])) for p in line]
dwg.add(dwg.polyline(points=pts, stroke='black', fill='none'))
dwg.save()
if b64Output:
_, buff = cv2.imencode('.jpg', out)
return base64.b64encode(buff)
else:
return
def rect(img, color=True, outname='rect', svgOutput=False, jpgOutput=False, b64Output=False):
# Resample image into pixel blocks
outname = cleanOutname(outname)
# Set up base image and output
gray = cv2.cvtColor(img['img'], cv2.COLOR_BGR2GRAY)
outshape = img['img'].shape if color else gray.shape
out = np.ones(outshape, np.uint8)*255
# Instantiate SVG Outpu
if svgOutput:
dwg = svgwrite.Drawing(outname['svg'], size=(img['w'],img['h']))
# Resample image by shrinking, then draw rectangles
mini = cv2.resize(img['img'], None, fx = 1/float(vg.INPUTS['RECT_SCALE']['val']), fy = 1/float(vg.INPUTS['RECT_SCALE']['val']), interpolation = cv2.INTER_AREA)
for i in range(0,mini.shape[0]):
for j in range(0,mini.shape[1]):
col = tuple([int(c) for c in mini[i,j]])
cv2.rectangle(out, (j*vg.INPUTS['RECT_SCALE']['val'],i*vg.INPUTS['RECT_SCALE']['val']), ((j+1)*vg.INPUTS['RECT_SCALE']['val'],(i+1)*vg.INPUTS['RECT_SCALE']['val']), col, -1)
if svgOutput:
fill = svgwrite.rgb(col[2],col[1],col[0]) if color else gbr2gray(col)
dwg.add(dwg.rect(insert=(j*vg.INPUTS['RECT_SCALE']['val'],i*vg.INPUTS['RECT_SCALE']['val']),size=(vg.INPUTS['RECT_SCALE']['val'],vg.INPUTS['RECT_SCALE']['val']),fill=fill))
# Output
if jpgOutput:
cv2.imwrite(outname['im'],out)
if svgOutput:
dwg.save()
if b64Output:
_, buff = cv2.imencode('.jpg', out)
return base64.b64encode(buff)
else:
return
def corners(img, outname='corners', color=True, svgOutput=False, jpgOutput=False, b64Output=False):
outname = cleanOutname(outname)
# Set up base image and output
gray = cv2.cvtColor(img['img'], cv2.COLOR_BGR2GRAY)
outshape = img['img'].shape if color else gray.shape
out = np.ones(outshape, np.uint8)*255
# Instantiate SVG Output
if svgOutput:
dwg = svgwrite.Drawing(outname['svg'], size=(img['w'],img['h']))
# Draw each triangle
triList = getTriangles(gray)
for t in triList:
pts = np.array([(t[0], t[1]), (t[2], t[3]), (t[4], t[5])]).reshape((-1,1,2)).astype(np.int32)
mask = np.zeros(gray.shape, np.uint8)
cv2.drawContours(mask, [pts], 0, 255, -1)
mean = cv2.mean(img['img'], mask = mask)
cv2.polylines(out, [pts], True, 0)
cv2.fillPoly(out, [pts], mean)
if svgOutput:
pts = [(int(p[0,0]),int(p[0,1])) for p in pts]
fill = svgwrite.rgb(mean[2],mean[1],mean[0]) if color else gbr2gray(mean)
dwg.add(dwg.polygon(points=pts,fill=fill))
# Finish up output
if jpgOutput:
cv2.imwrite(outname['im'],out)
if svgOutput:
dwg.save()
if b64Output:
_, buff = cv2.imencode('.jpg', out)
return base64.b64encode(buff)
else:
return
def hatch(img,outname='hatch',randAngle=True,angle=70,svgOutput=False, jpgOutput=False, b64Output=False):
# Setup transformation matrix early
Q = lambda theta: np.matrix([[math.cos(theta), -math.sin(theta)],[math.sin(theta),math.cos(theta)]])
Q = Q if randAngle else Q(angle)
outname = cleanOutname(outname)
# Setup base image and output
gray = cv2.cvtColor(img['img'], cv2.COLOR_BGR2GRAY)
out = np.ones(gray.shape, np.uint8)*255
# Instantiate SVG Output
if svgOutput:
dwg = svgwrite.Drawing(outname['svg'], size=(img['w'],img['h']))
# Process through each triangle
i = 0 # Unfortunately need a counter for SVG clip path id's
triList = getTriangles(gray)
for t in triList:
i = i+1
pts = np.array([(t[0], t[1]), (t[2], t[3]), (t[4], t[5])]).reshape((-1,1,2)).astype(np.int32)
mask = np.zeros(gray.shape, np.uint8)
cv2.drawContours(mask, [pts], 0, 255, -1)
x,y,w,h = cv2.boundingRect(pts)
maxDim = max(w,h)
numLines = int(cv2.mean(gray, mask = mask)[0]/(255*w*h) * vg.INPUTS['HATCH_DENSITY']['val'])
if (numLines > 1):
temp = np.zeros(gray.shape, np.uint8)
R = Q(randint(0,90)) if randAngle else Q
c = np.array([x+w/2,y+h/2])
verts = np.linspace(y-2*maxDim,y+2*maxDim,6*numLines)
lines = [[c+np.dot(R,np.array([x-maxDim,int(y)])-c), c+np.dot(R,np.array([x+2*maxDim,int(y)])-c)] for y in verts]
cv2.polylines(out, [pts], True, 0)
if svgOutput:
pts = [(int(p[0,0]),int(p[0,1])) for p in pts]
dwg.add(dwg.polyline(points=pts, stroke='black', fill='none'))
clip = dwg.defs.add(dwg.clipPath(id='cp_'+str(i)))
clip.add(dwg.polyline(points=pts))
for line in lines:
p1 = (int(line[0][0,0]), int(line[0][0,1]))
p2 = (int(line[1][0,0]), int(line[1][0,1]))
cv2.line(temp, p1, p2, 1)
if svgOutput:
dwg.add(dwg.line(start=p1,end=p2,stroke='black',clip_path='url(#cp_'+str(i)+')'))
out = out + cv2.bitwise_and(mask,temp)
# Output
if jpgOutput:
cv2.imwrite(outname['im'],out)
if svgOutput:
dwg.save()
if b64Output:
_, buff = cv2.imencode('.jpg', out)
return base64.b64encode(buff)
else:
return
def waves(img, outname='waves', svgOutput=False, jpgOutput=False, b64Output=False):
outname = cleanOutname(outname)
# Resize and Resample Image
gray = cv2.cvtColor(img['img'], cv2.COLOR_BGR2GRAY)
mini = cv2.resize(gray, (img['w'],vg.INPUTS['SIN_COUNT']['val']), interpolation = cv2.INTER_AREA)
inv_mini = cv2.bitwise_not(mini)
out = np.ones(gray.shape, np.uint8)*255
# Instantiate SVG Output
if svgOutput:
dwg = svgwrite.Drawing(outname['svg'], size=(img['w'],img['h']))
xs = np.arange(0,mini.shape[1])
ys = (inv_mini/float(255))*vg.INPUTS['SIN_HEIGHT']['val']*np.sin(xs*2*math.pi/vg.INPUTS['SIN_LENGTH']['val']) + (np.matrix(range(0,mini.shape[0])).T + 0.5)*img['h']/mini.shape[0]
for row in ys:
pts = list(zip(xs, np.asarray(row).squeeze()))
pts = np.array(pts).reshape((-1,1,2)).astype(np.int32)
cv2.polylines(out, [pts], False, 0)
pts = [(int(p[0,0]),int(p[0,1])) for p in pts]
if svgOutput:
dwg.add(dwg.polyline(points=pts, stroke='black', fill='none'))
if jpgOutput:
cv2.imwrite(outname['im'],out)
if svgOutput:
dwg.save()
if b64Output:
_, buff = cv2.imencode('.jpg', out)
return base64.b64encode(buff)
else:
return
methods = {"Edges": edges, "Pixilize": rect, "Waves": waves, "Hatch": hatch, "Corners": corners}