55from svg .file import SVGFileV2
66from svg .basic import random_color , color_fader , draw_circle , draw_rect , random_color_hsv
77from svg .basic import convert_rgb , draw_any , clip_float , draw_path
8+ from svg .basic import draw_only_path , add_style , get_styles
89from svg .geo_transformation import translation_pts , translation_pts_xy
910from svg .geo_transformation import zoom_pts_xy , split_points , combine_xy , zoom_non_pts_xy
1011from svgSmile import drawSmileSVG
@@ -45,7 +46,8 @@ def blur_img(img, size=(5, 5)):
4546
4647
4748def 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
6567class 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
120126def maskImage ():
@@ -180,14 +186,15 @@ def drawPointsRect_colors(svg, pts, r, colors):
180186
181187
182188def 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
321330def 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
341352def 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
356376def 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
368389if __name__ == '__main__' :
0 commit comments