File size: 8,872 Bytes
0af58b0 |
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 |
import gradio as gr
import os
import tempfile
from PIL import Image
import subprocess
import logging
import base64
import xml.etree.ElementTree as ET
from concurrent.futures import ThreadPoolExecutor, as_completed
import re
from iconsheet import icon_sheet_interface
import mimetypes
# Set up logging
logging.basicConfig(filename='svgmaker.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')
def process_single_image(image_file, stroke_width, fill, stroke, opacity, output_size, output_dir):
try:
logging.info(f"Processing image: {image_file.name}")
with Image.open(image_file.name) as image:
# Calculate new dimensions maintaining aspect ratio
aspect_ratio = image.width / image.height
if aspect_ratio > 1:
new_width = output_size
new_height = int(output_size / aspect_ratio)
else:
new_height = output_size
new_width = int(output_size * aspect_ratio)
logging.info(f"Resizing image to {new_width}x{new_height}")
# Resize image
image = image.resize((new_width, new_height), Image.LANCZOS)
# Convert to grayscale
image = image.convert('L')
# Save as temporary PGM file
with tempfile.NamedTemporaryFile(suffix='.pgm', delete=False) as temp_pgm:
image.save(temp_pgm.name)
temp_pgm_path = temp_pgm.name
# Run potrace with improved settings
output_svg = os.path.join(output_dir, f"{os.path.splitext(os.path.basename(image_file.name))[0]}.svg")
subprocess.run([
"potrace",
"--svg",
"--output", output_svg,
"--turdsize", "2",
"--alphamax", "1",
"--opttolerance", "0.2",
"--unit", "10",
temp_pgm_path
], check=True)
# Clean up temporary PGM
os.unlink(temp_pgm_path)
# Modify SVG with user-specified styles and remove unnecessary elements
with open(output_svg, 'r') as f:
svg_content = f.read()
# Remove namespace prefixes and metadata
svg_content = re.sub(r'<ns0:', '<', svg_content)
svg_content = re.sub(r'</ns0:', '</', svg_content)
svg_content = re.sub(r'xmlns:ns0="[^"]+"', '', svg_content)
svg_content = re.sub(r'<metadata>.*?</metadata>', '', svg_content, flags=re.DOTALL)
# Remove unnecessary group transformation
svg_content = re.sub(r'<g transform="[^"]+">', '<g>', svg_content)
# Update SVG attributes
svg_tree = ET.fromstring(svg_content)
svg_tree.set('width', str(new_width))
svg_tree.set('height', str(new_height))
svg_tree.set('viewBox', f"0 0 {new_width} {new_height}")
for path in svg_tree.findall('.//{http://www.w3.org/2000/svg}path'):
path.set('fill', fill)
path.set('fill-opacity', str(opacity))
path.set('stroke', stroke)
path.set('stroke-width', str(stroke_width))
path.set('stroke-opacity', str(opacity))
svg_content = ET.tostring(svg_tree, encoding='unicode')
# Final cleanup
svg_content = re.sub(r'\s+', ' ', svg_content) # Remove excess whitespace
svg_content = re.sub(r'> <', '><', svg_content) # Remove space between tags
with open(output_svg, 'w') as f:
f.write(svg_content)
# Create a base64 encoded version of the SVG for preview
svg_base64 = base64.b64encode(svg_content.encode('utf-8')).decode('utf-8')
preview = f"data:image/svg+xml;base64,{svg_base64}"
return output_svg, preview
except Exception as e:
logging.error(f"Error processing image {image_file.name}: {str(e)}")
return None, None
def vectorize_icons(images, stroke_width, fill, stroke, opacity, output_size, output_dir, progress=gr.Progress()):
vectorized_icons = []
svg_previews = []
total_images = len(images)
logging.info(f"Vectorizing {total_images} images")
os.makedirs(output_dir, exist_ok=True)
with ThreadPoolExecutor() as executor:
future_to_image = {executor.submit(process_single_image, image, stroke_width, fill, stroke, opacity, output_size, output_dir): image for image in images}
for i, future in enumerate(as_completed(future_to_image)):
image = future_to_image[future]
progress((i + 1) / total_images, f"Vectorizing image {i+1}/{total_images}")
try:
result, preview = future.result()
vectorized_icons.append(result)
svg_previews.append(preview)
logging.info(f"Successfully vectorized image {i+1}")
progress((i + 1) / total_images, f"Vectorized image {i+1}/{total_images}")
except Exception as e:
logging.error(f"Error vectorizing icon {image.name}: {str(e)}")
vectorized_icons.append(None)
svg_previews.append(None)
progress((i + 1) / total_images, f"Failed to vectorize image {i+1}/{total_images}: {str(e)}")
progress(1.0, "Vectorization complete")
logging.info(f"Vectorization complete. Successful: {len([i for i in vectorized_icons if i is not None])}, Failed: {len([i for i in vectorized_icons if i is None])}")
return vectorized_icons, svg_previews
def create_preview_grid(svg_previews):
# Dynamically create grid based on number of SVG previews
num_images = len([preview for preview in svg_previews if preview is not None])
# Define the number of columns for the grid
columns = 4
grid_html = f'<div style="display: grid; grid-template-columns: repeat({columns}, 1fr); gap: 10px; background-color: #f0f0f0; padding: 20px; border-radius: 10px;">'
for preview in svg_previews:
if preview:
grid_html += f'''
<div style="background-color: white; padding: 10px; border-radius: 5px;">
<img src="{preview}" style="width: 100%; height: auto;" onclick="this.classList.toggle('zoomed')" class="zoomable">
</div>
'''
else:
grid_html += '<div style="background-color: white; padding: 10px; border-radius: 5px;"><p>Error</p></div>'
grid_html += '</div>'
# Add CSS for zoom functionality
grid_html += '''
<style>
.zoomable { transition: transform 0.3s ease; }
.zoomable.zoomed { transform: scale(2.5); z-index: 1000; position: relative; }
</style>
'''
return grid_html
with gr.Blocks() as app:
gr.Markdown("# SVG Maker and Icon Sheet Generator")
with gr.Tabs():
with gr.TabItem("SVG Maker"):
gr.Markdown("# SVG Maker")
with gr.Row():
image_input = gr.File(label="Upload Images", file_count="multiple")
with gr.Row():
stroke_width = gr.Slider(0, 10, label="Stroke Width", value=1)
fill = gr.ColorPicker(label="Fill Color", value="#000000")
stroke = gr.ColorPicker(label="Stroke Color", value="#000000")
opacity = gr.Slider(0, 1, label="Opacity", value=1, step=0.1)
with gr.Row():
output_size = gr.Slider(32, 1024, label="Output Size (px)", value=512, step=32)
output_dir = gr.Textbox(label="Output Directory", value="output", placeholder="Enter output directory path")
vectorize_btn = gr.Button("Vectorize")
svg_output = gr.File(label="Vectorized Icons", file_count="multiple")
svg_preview = gr.HTML(label="SVG Previews")
def process_and_display(images, stroke_width, fill, stroke, opacity, output_size, output_dir):
vectorized_icons, svg_previews = vectorize_icons(images, stroke_width, fill, stroke, opacity, output_size, output_dir)
preview_html = create_preview_grid(svg_previews)
# Filter out None values from vectorized_icons
valid_icons = [icon for icon in vectorized_icons if icon is not None]
return valid_icons, preview_html
vectorize_btn.click(process_and_display,
inputs=[image_input, stroke_width, fill, stroke, opacity, output_size, output_dir],
outputs=[svg_output, svg_preview])
with gr.TabItem("Icon Sheet"):
mimetypes.add_type('image/svg+xml', '.svg')
icon_sheet_interface()
if __name__ == "__main__":
try:
app.launch()
except Exception as e:
logging.error(f"Failed to launch app: {str(e)}")
raise |