Skip to content

Commit fe4fd6c

Browse files
refactored
1 parent 151c179 commit fe4fd6c

3 files changed

Lines changed: 54 additions & 29 deletions

File tree

src/svg/basic.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Author: Steven Huang, Auckland, NZ
44
# License: MIT License
55
"""
6-
Description: Basic svg functions and svg node(string) generate functions.
6+
Description: Basic svg functions and svg node(string) generating functions.
77
"""
88

99
import random
@@ -196,7 +196,8 @@ def draw_any(tag, text=None, **kwargs):
196196
draw_any(tagName, text, **attrDict)
197197
"""
198198

199-
attri_list = [(str(key) + '=' + '"' + str(value) + '"') for key, value in kwargs.items()]
199+
attri_list = [(str(key) + '=' + '"' + str(value) + '"')
200+
for key, value in kwargs.items()]
200201
attris = ' '.join(attri_list)
201202
# print('attris=', attris)
202203
if text is not None:

src/svg/file.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
class SVGFileV2:
1717
"""lxml version svg"""
1818

19+
_url = 'http://www.w3.org/2000/svg'
20+
_xlink = 'http://www.w3.org/1999/xlink'
21+
_namespace = 'http://www.w3.org/XML/1998/namespace'
22+
_version = '1.1'
23+
1924
def __init__(self, file, W=100, H=100, title=None, border=False,
2025
border_color='black', border_width=1):
2126
self._file = file
2227
self._width = W
2328
self._height = H
24-
self._url = "http://www.w3.org/2000/svg"
25-
self._xlink = "http://www.w3.org/1999/xlink"
26-
# self.namespace = "http://www.w3.org/XML/1998/namespace"
27-
self._version = "1.1"
28-
self._root = etree.Element("svg", nsmap={None: self._url, "xlink": self._xlink},
29-
version=self._version)
29+
30+
# self.
31+
self._root = etree.Element("svg", nsmap={
32+
None: SVGFileV2._url, "xlink": SVGFileV2._xlink}, version=SVGFileV2._version)
3033

3134
self.set_node(self._root, 'width', f'{self._width}')
3235
self.set_node(self._root, 'height', f'{self._height}')

src/svgImageMask.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from svg.file import SVGFileV2
66
from svg.basic import random_color, color_fader, draw_circle, draw_rect, random_color_hsv
77
from svg.basic import convert_rgb, draw_any, clip_float, draw_path
8+
from svg.basic import draw_only_path, add_style, get_styles
89
from svg.geo_transformation import translation_pts, translation_pts_xy
910
from svg.geo_transformation import zoom_pts_xy, split_points, combine_xy, zoom_non_pts_xy
1011
from svgSmile import drawSmileSVG
@@ -45,7 +46,8 @@ def blur_img(img, size=(5, 5)):
4546

4647

4748
def OtsuMethodThresHold(img):
48-
_, threshold = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
49+
_, threshold = cv2.threshold(
50+
img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
4951
return threshold
5052

5153

@@ -64,14 +66,16 @@ def showimage(img, str='image', autoSize=False):
6466

6567
class SVGImageMask:
6668
def __init__(self, imageFile, dstSvgfile, step=1):
67-
self.image = loadImg(imageFile, cv2.IMREAD_COLOR) # cv2.IMREAD_GRAYSCALE
69+
# cv2.IMREAD_GRAYSCALE
70+
self.image = loadImg(imageFile, cv2.IMREAD_COLOR)
6871
self.height = self.image.shape[0]
6972
self.width = self.image.shape[1]
7073
self.step = step
7174
self.svgH = int((self.height // step) * step)
7275
self.svgW = int((self.width // step) * step)
7376
self.svg = SVGFileV2(dstSvgfile, W=self.svgW, H=self.svgH)
74-
print('step=', step, 'image H,W=', self.height, self.width, 'SVG H,W=', self.svgH, self.svgW)
77+
print('step=', step, 'image H,W=', self.height,
78+
self.width, 'SVG H,W=', self.svgH, self.svgW)
7579

7680
def drawStep(self):
7781
r = self.step / 2
@@ -86,7 +90,8 @@ def drawStep(self):
8690
if 0:
8791
self.svg.draw(draw_circle(y, x, r, color=color))
8892
else:
89-
self.svg.draw(draw_rect(y, x, self.step, self.step, color=color))
93+
self.svg.draw(
94+
draw_rect(y, x, self.step, self.step, color=color))
9095

9196
def get_coordinates_color(self):
9297
coords = []
@@ -114,7 +119,8 @@ def drawColor(self):
114119
for i in range(0, self.svgH, self.step):
115120
for j in range(0, self.svgW, self.step):
116121
color = convert_rgb(self.image[i, j, :])
117-
self.svg.draw(draw_rect(j, i, r, r, stroke_width=0, color=color))
122+
self.svg.draw(
123+
draw_rect(j, i, r, r, stroke_width=0, color=color))
118124

119125

120126
def maskImage():
@@ -180,14 +186,15 @@ def drawPointsRect_colors(svg, pts, r, colors):
180186

181187

182188
def my_path_potrace(paths, N=2):
183-
print('len(path)=', len(paths))
189+
# print('len(path)=', len(paths))
184190

185191
# Iterate over path curves
186192
for curve in paths:
187193
start_pt = curve.start_point
188194
# pts = curve.decomposition_points
189195
# print('pts=', pts)
190-
path = 'M %f %f ' % (clip_float(start_pt.x, N), clip_float(start_pt.y, N))
196+
path = 'M %f %f ' % (clip_float(start_pt.x, N),
197+
clip_float(start_pt.y, N))
191198
# print("start_point =", start_pt)
192199

193200
for segment in curve:
@@ -286,7 +293,8 @@ def path_potrace(path):
286293
a = segment.c1
287294
b = segment.c2
288295
c = segment.end_point
289-
parts.append("C%f,%f %f,%f %f,%f" % (a.x, a.y, b.x, b.y, c.x, c.y))
296+
parts.append("C%f,%f %f,%f %f,%f" %
297+
(a.x, a.y, b.x, b.y, c.x, c.y))
290298
parts.append("z")
291299

292300
return "".join(parts)
@@ -315,7 +323,8 @@ def image_svg_path(file, dst_file):
315323
path = path_potrace_jagged(paths)
316324
print('len(path)=', len(path))
317325
fill_color = random_color_hsv() # 'black'
318-
svg.draw(draw_path(path, stroke_width=1.8, color='none', fill_color=fill_color, fill_rule='evenodd'))
326+
svg.draw(draw_path(path, stroke_width=1.8, color='none',
327+
fill_color=fill_color, fill_rule='evenodd'))
319328

320329

321330
def image_svg_path2(file, dst_file):
@@ -333,24 +342,35 @@ def image_svg_path2(file, dst_file):
333342
for i in range(N):
334343
for j in range(N):
335344
to_point = (i * W / N, j * H / N)
336-
path = path_potrace_jagged_trans(paths, zoom_x=zoom, zoom_y=zoom, to_point=to_point)
345+
path = path_potrace_jagged_trans(
346+
paths, zoom_x=zoom, zoom_y=zoom, to_point=to_point)
337347
fill_color = random_color()
338-
svg.draw(draw_path(path, color='none', fill_color=fill_color, fill_rule='evenodd'))
348+
svg.draw(draw_path(path, color='none',
349+
fill_color=fill_color, fill_rule='evenodd'))
339350

340351

341352
def image_svg_path3(file, dst_file):
342353
"""potrace to multi <path/> elements
343354
"""
344355
image = get_binary_image(file)
345-
svg = SVGFileV2(dst_file, W=image.shape[1], H=image.shape[0], border=True)
356+
svg = SVGFileV2(dst_file, W=image.shape[1], H=image.shape[0], border=False)
346357

347358
paths = get_potrace_path(image)
348-
for i, path in enumerate(my_path_potrace(paths)):
349-
print(f'[{i}]', 'path=', path)
350-
svg.draw(draw_path(path, stroke_width=1.8, color='none', fill_color='#000000', fill_rule='evenodd'))
351-
# f.png example image
352-
# M 71.640000 46.500000 right eye circle
353-
# M 66.000000 39.000000 right eyeball
359+
if 0:
360+
for i, path in enumerate(my_path_potrace(paths)):
361+
print(f'[{i}]', 'path=', path)
362+
svg.draw(draw_path(path, stroke_width=1.8, color='None',
363+
fill_color='#000000', fill_rule='evenodd'))
364+
else:
365+
styleDict = {}
366+
styleDict['stroke_width'] = '1.8'
367+
styleDict['color'] = 'None'
368+
styleDict['fill_color'] = '#000000'
369+
styleDict['fill_rule'] = 'evenodd'
370+
svg.draw(add_style('path', get_styles(styleDict)))
371+
372+
for i, path in enumerate(my_path_potrace(paths)):
373+
svg.draw(draw_only_path(path))
354374

355375

356376
def main():
@@ -359,10 +379,11 @@ def main():
359379
# imgSvgElement()
360380
# image2_svg()
361381

362-
file = r'.\res\Lenna.png' # r'.\res\f.png' #
363-
dst_file = os.path.join(gImageOutputPath, 'image_path.svg')
382+
file = r'.\res\Lenna.png' # r'.\res\f.png'
383+
dst_file = os.path.join(gImageOutputPath, 'image_path3.svg')
364384
# image_svg_path(file, dst_file)
365-
image_svg_path2(file, dst_file)
385+
# image_svg_path2(file, dst_file)
386+
image_svg_path3(file, dst_file)
366387

367388

368389
if __name__ == '__main__':

0 commit comments

Comments
 (0)