umuth commited on
Commit
17689de
·
verified ·
1 Parent(s): e4c43fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -43
app.py CHANGED
@@ -44,55 +44,15 @@ def process_single_image(image_file, stroke_width, fill, stroke, opacity, output
44
  output_svg = os.path.join(output_dir, f"{os.path.splitext(os.path.basename(image_file.name))[0]}.svg")
45
  subprocess.run([
46
  "potrace",
 
47
  "--svg",
48
- "--output", output_svg,
49
- "--turdsize", "2",
50
- "--alphamax", "1",
51
- "--opttolerance", "0.2",
52
- "--unit", "10",
53
- temp_pgm_path
54
  ], check=True)
55
 
56
  # Clean up temporary PGM
57
  os.unlink(temp_pgm_path)
58
 
59
- # Modify SVG with user-specified styles and remove unnecessary elements
60
- with open(output_svg, 'r') as f:
61
- svg_content = f.read()
62
-
63
- # Remove namespace prefixes and metadata
64
- svg_content = re.sub(r'<ns0:', '<', svg_content)
65
- svg_content = re.sub(r'</ns0:', '</', svg_content)
66
- svg_content = re.sub(r'xmlns:ns0="[^"]+"', '', svg_content)
67
- svg_content = re.sub(r'<metadata>.*?</metadata>', '', svg_content, flags=re.DOTALL)
68
-
69
- # Remove unnecessary group transformation
70
- svg_content = re.sub(r'<g transform="[^"]+">', '<g>', svg_content)
71
-
72
- # Update SVG attributes
73
- svg_tree = ET.fromstring(svg_content)
74
- svg_tree.set('width', str(new_width))
75
- svg_tree.set('height', str(new_height))
76
- svg_tree.set('viewBox', f"0 0 {new_width} {new_height}")
77
- for path in svg_tree.findall('.//{http://www.w3.org/2000/svg}path'):
78
- path.set('fill', fill)
79
- path.set('fill-opacity', str(opacity))
80
- path.set('stroke', stroke)
81
- path.set('stroke-width', str(stroke_width))
82
- path.set('stroke-opacity', str(opacity))
83
-
84
- svg_content = ET.tostring(svg_tree, encoding='unicode')
85
-
86
- # Final cleanup
87
- svg_content = re.sub(r'\s+', ' ', svg_content) # Remove excess whitespace
88
- svg_content = re.sub(r'> <', '><', svg_content) # Remove space between tags
89
-
90
- with open(output_svg, 'w') as f:
91
- f.write(svg_content)
92
-
93
- # Create a base64 encoded version of the SVG for preview
94
- svg_base64 = base64.b64encode(svg_content.encode('utf-8')).decode('utf-8')
95
- preview = f"data:image/svg+xml;base64,{svg_base64}"
96
 
97
  return output_svg, preview
98
  except Exception as e:
 
44
  output_svg = os.path.join(output_dir, f"{os.path.splitext(os.path.basename(image_file.name))[0]}.svg")
45
  subprocess.run([
46
  "potrace",
47
+ temp_pgm_path,
48
  "--svg",
49
+ "--o", output_svg
 
 
 
 
 
50
  ], check=True)
51
 
52
  # Clean up temporary PGM
53
  os.unlink(temp_pgm_path)
54
 
55
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  return output_svg, preview
58
  except Exception as e: