-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.py
executable file
·290 lines (224 loc) · 9.26 KB
/
convert.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/usr/bin/python
from PIL import Image
import svgwrite
from svgwrite.path import Path
import random
shapes = [
'Circle',
'Crosshatch',
'CrosshatchRandom',
'Zigzag',
'Squiggle',
'Sine',
]
class Block(object):
def __init__(self, image, x_offset, y_offset, block_size=8, alpha=1.0, filter_ratio=0.1):
self.image = image
self.x_offset = x_offset
self.y_offset = y_offset
self.block_size = block_size
self.alpha = max(alpha, 0.001)
self.filter_ratio = filter_ratio
self.intensity = self.getIntensity()
def getIntensity(self):
width, height = self.image.size
pixels = []
# Fill pixels list with pixel values for block
for y in range(self.y_offset, self.y_offset + self.block_size):
for x in range(self.x_offset, self.x_offset + self.block_size):
if x < width and y < height:
pixels.append(self.image.getpixel((x,y)))
# Determine average value of pixels in block
if pixels:
intensity = (255.0 - (sum(pixels) / len(pixels))) / (255.0 * self.alpha)
else:
intensity = 0
# If alpha value causes the radius to exceed halfblock size, radius should be set to halfblock size
return intensity
# Stub to be overridden by subclasses
def draw(self, svg_file):
pass
@property
def left(self):
return self.x_offset
@property
def top(self):
return self.y_offset
@property
def right(self):
return self.x_offset + self.block_size
@property
def bottom(self):
return self.y_offset + self.block_size
@property
def mid_x(self):
return self.x_offset + (self.block_size / 2)
@property
def mid_y(self):
return self.y_offset + (self.block_size / 2)
class Circle(Block):
def draw(self, svg_file):
if self.intensity < self.filter_ratio:
return
rad = self.block_size/2.0
radius = min(rad * self.intensity, rad)
svg_file.add(svg_file.circle((self.x_offset + (self.block_size / 2), self.y_offset + (self.block_size/2)), radius))
class Crosshatch(Block):
def draw(self, svg_file, max_lines=12):
if self.intensity < self.filter_ratio:
return
path = Path(("M", self.left, self.top), stroke="black", stroke_width="0.3", fill="none")
count = int((max_lines * self.intensity))
left_third = self.left + (self.block_size / 4.0)
right_third = self.left + (3 * (self.block_size / 4.0))
top_third = self.top + (self.block_size / 4.0)
bottom_third = self.top + (3 * (self.block_size / 4.0))
for m in range(count):
if m == 0:
path.push("L", self.right, self.bottom)
path.push("M", self.right, self.top)
elif m == 1:
path.push("L", self.left, self.bottom)
path.push("M", self.mid_x, self.top)
elif m == 2:
path.push("L", self.mid_x, self.bottom)
path.push("M", self.left, self.mid_y)
elif m == 3:
path.push("L", self.right, self.mid_y)
path.push("M", left_third, self.top)
elif m == 4:
path.push("L", left_third, self.bottom)
path.push("M", right_third, self.top)
elif m == 5:
path.push("L", right_third, self.bottom)
path.push("M", self.left, top_third)
elif m == 6:
path.push("L", self.right, top_third)
path.push("M", self.left, bottom_third)
elif m == 7:
path.push("L", self.right, bottom_third)
path.push("M", left_third, self.top)
elif m == 8:
path.push("L", self.right, bottom_third)
path.push("M", right_third, self.top)
elif m == 9:
path.push("L", self.left, bottom_third)
path.push("M", left_third, self.bottom)
elif m == 10:
path.push("L", self.right, top_third)
path.push("M", right_third, self.bottom)
elif m == 11:
path.push("L", self.left, top_third)
svg_file.add(path)
class CrosshatchRandom(Block):
def draw(self, svg_file, max_lines=12):
if self.intensity < self.filter_ratio:
return
count = int((max_lines * self.intensity) + 0.5)
path = None
for _ in range(count):
start_side = random.choice((self.left, self.top))
offset = random.random() * self.block_size
if start_side == self.left:
x1 = self.left
x2 = self.right
y1 = self.top + offset
y2 = self.bottom - offset
else:
x1 = self.left + offset
x2 = self.right - offset
y1 = self.top
y2 = self.bottom
if path is None:
path = Path(("M", x1, y1), stroke="black", stroke_width="0.3", fill="none")
else:
path.push("M", x1, y1)
path.push("L", x2, y2)
svg_file.add(path)
class Zigzag(Block):
def draw(self, svg_file):
if self.intensity < self.filter_ratio:
return
lines = int(max(min(30 * self.intensity, 40), 1))
y_disp = float(self.block_size) / lines
start = (self.left, self.top)
end = (self.left, self.top)
path = Path(("M", self.left, self.top), stroke="black", stroke_width="0.3", fill="none")
# Stop when we hit the bottom
while start[1] < self.bottom:
# Determine which edge we are on
# Left
if start[0] == self.left:
end = (self.right, start[1] + y_disp)
# right
elif start[0] == self.right:
end = (self.left, start[1] + y_disp)
if end[1] < self.bottom:
# Recalc line to terminate at bottom
path.push('L', *end)
start = end
svg_file.add(path)
class Squiggle(Block):
def draw(self, svg_file):
if self.intensity < self.filter_ratio:
return
segments = int(max(min(20 * self.intensity, 30), 1))
def rand_point():
return random.uniform(self.left, self.right), random.uniform(self.top, self.bottom)
start = rand_point()
path = Path(("M",) + start, stroke="black", stroke_width="0.3", fill="none")
for _ in range(segments):
path.push("T", *rand_point())
svg_file.add(path)
class Sine(Block):
def draw(self, svg_file, max_waves=20):
if self.intensity < self.filter_ratio:
return
waves = int(max(min(max_waves * self.intensity, max_waves*1.5), 1))
peak_offset = self.block_size * 0.8
step_width = (self.block_size / (waves * 4.0))
path = Path(("M", self.left, self.mid_y), stroke="black", stroke_width="0.3", fill="none")
for _ in range(waves):
path.push("q", step_width, -peak_offset, 2 * step_width, 0)
path.push("q", step_width, peak_offset, 2 * step_width, 0)
svg_file.add(path)
def convert(img_file, shape_name='Circle', block_size=8, alpha_value=1.0, filter_limit=0.1, outfile=None):
'''
Converts a raster image to a SVG image by converting blocks of pixels to an SVG circle
with a radius based upon the average pixel values of the block.
Arguments:
img_file: path to file (str) or Python file object (file)
block_size: size of block for processing (int)
alpha_value: modifying the alpha_value allows for changing the level of contrast for images (float)
filter_limit: rendered circles with a radius below the filter_limit are not drawn at all (float)
outfile: path/name of output file for writing the SVG file (string)
'''
# Set defaults
if not outfile:
outfile = "%s.svg" % outfile
# Open and prepare raster image file
image = Image.open(img_file)
image = image.convert("L")
# Initiate variables
width, height = image.size
x, y = 0, 0
# Get shape from shape_name
shape = globals().get(shape_name, None)
if shape is None:
raise Exception("Shape %s does not exist." % shape_name)
# Open and prepare SVG output file
svg = svgwrite.Drawing(outfile, size=('100%', '100%'), profile="full", viewBox="0 0 %d %d" % (width, height), )
# Process image in blocks until all
while x < width and y < height:
block = shape(image, x, y, block_size, alpha_value)
block.draw(svg)
# Delete block to save memory in large files
del block
# Move to next block
x += block_size
if x >= width:
x = 0
y += block_size
# Save SVG file and return SVG file name
svg.save()
return outfile