Update app.py
Browse files
app.py
CHANGED
|
@@ -17,6 +17,9 @@ logging.basicConfig(filename='svgmaker.log', level=logging.INFO,
|
|
| 17 |
|
| 18 |
def process_single_image(image_file, stroke_width, fill, stroke, opacity, output_size, output_dir):
|
| 19 |
try:
|
|
|
|
|
|
|
|
|
|
| 20 |
logging.info(f"Processing image: {image_file.name}")
|
| 21 |
with Image.open(image_file.name) as image:
|
| 22 |
# Calculate new dimensions maintaining aspect ratio
|
|
@@ -46,13 +49,17 @@ def process_single_image(image_file, stroke_width, fill, stroke, opacity, output
|
|
| 46 |
"potrace",
|
| 47 |
temp_pgm_path,
|
| 48 |
"--svg",
|
| 49 |
-
"-
|
| 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:
|
|
@@ -60,6 +67,9 @@ def process_single_image(image_file, stroke_width, fill, stroke, opacity, output
|
|
| 60 |
return None, None
|
| 61 |
|
| 62 |
def vectorize_icons(images, stroke_width, fill, stroke, opacity, output_size, output_dir, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
| 63 |
vectorized_icons = []
|
| 64 |
svg_previews = []
|
| 65 |
total_images = len(images)
|
|
@@ -144,6 +154,9 @@ with gr.Blocks() as app:
|
|
| 144 |
svg_preview = gr.HTML(label="SVG Previews")
|
| 145 |
|
| 146 |
def process_and_display(images, stroke_width, fill, stroke, opacity, output_size, output_dir):
|
|
|
|
|
|
|
|
|
|
| 147 |
vectorized_icons, svg_previews = vectorize_icons(images, stroke_width, fill, stroke, opacity, output_size, output_dir)
|
| 148 |
preview_html = create_preview_grid(svg_previews)
|
| 149 |
# Filter out None values from vectorized_icons
|
|
@@ -163,4 +176,4 @@ if __name__ == "__main__":
|
|
| 163 |
app.launch()
|
| 164 |
except Exception as e:
|
| 165 |
logging.error(f"Failed to launch app: {str(e)}")
|
| 166 |
-
raise
|
|
|
|
| 17 |
|
| 18 |
def process_single_image(image_file, stroke_width, fill, stroke, opacity, output_size, output_dir):
|
| 19 |
try:
|
| 20 |
+
if image_file is None:
|
| 21 |
+
raise ValueError("No image file provided.")
|
| 22 |
+
|
| 23 |
logging.info(f"Processing image: {image_file.name}")
|
| 24 |
with Image.open(image_file.name) as image:
|
| 25 |
# Calculate new dimensions maintaining aspect ratio
|
|
|
|
| 49 |
"potrace",
|
| 50 |
temp_pgm_path,
|
| 51 |
"--svg",
|
| 52 |
+
"-o", output_svg
|
| 53 |
], check=True)
|
| 54 |
|
| 55 |
# Clean up temporary PGM
|
| 56 |
os.unlink(temp_pgm_path)
|
| 57 |
|
| 58 |
+
# Create a base64 encoded version of the SVG for preview
|
| 59 |
+
with open(output_svg, 'r') as f:
|
| 60 |
+
svg_content = f.read()
|
| 61 |
+
svg_base64 = base64.b64encode(svg_content.encode('utf-8')).decode('utf-8')
|
| 62 |
+
preview = f"data:image/svg+xml;base64,{svg_base64}"
|
| 63 |
|
| 64 |
return output_svg, preview
|
| 65 |
except Exception as e:
|
|
|
|
| 67 |
return None, None
|
| 68 |
|
| 69 |
def vectorize_icons(images, stroke_width, fill, stroke, opacity, output_size, output_dir, progress=gr.Progress()):
|
| 70 |
+
if images is None or len(images) == 0:
|
| 71 |
+
raise ValueError("No images were uploaded. Please upload at least one image.")
|
| 72 |
+
|
| 73 |
vectorized_icons = []
|
| 74 |
svg_previews = []
|
| 75 |
total_images = len(images)
|
|
|
|
| 154 |
svg_preview = gr.HTML(label="SVG Previews")
|
| 155 |
|
| 156 |
def process_and_display(images, stroke_width, fill, stroke, opacity, output_size, output_dir):
|
| 157 |
+
if images is None or len(images) == 0:
|
| 158 |
+
raise ValueError("No images were uploaded. Please upload at least one image.")
|
| 159 |
+
|
| 160 |
vectorized_icons, svg_previews = vectorize_icons(images, stroke_width, fill, stroke, opacity, output_size, output_dir)
|
| 161 |
preview_html = create_preview_grid(svg_previews)
|
| 162 |
# Filter out None values from vectorized_icons
|
|
|
|
| 176 |
app.launch()
|
| 177 |
except Exception as e:
|
| 178 |
logging.error(f"Failed to launch app: {str(e)}")
|
| 179 |
+
raise
|