eps2svg / app.py
namelessai's picture
Update app.py
53d899a verified
import gradio as gr
from wand.image import Image
import tempfile
import os
import subprocess
def convert_eps_to_svg(eps_file):
"""
Convert EPS file to SVG using multiple methods
"""
if eps_file is None:
return None, "Please upload an EPS file"
try:
# Create temporary file for output
with tempfile.NamedTemporaryFile(delete=False, suffix='.svg') as tmp_svg:
svg_path = tmp_svg.name
# Method 1: Try using Inkscape (best for vector preservation)
try:
result = subprocess.run(
['inkscape', eps_file.name, '--export-type=svg', f'--export-filename={svg_path}'],
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0 and os.path.exists(svg_path) and os.path.getsize(svg_path) > 0:
return svg_path, "Conversion successful using Inkscape! Download your SVG file below."
except Exception as e:
print(f"Inkscape method failed: {e}")
# Method 2: Try using Ghostscript to convert EPS to PDF, then PDF to SVG
try:
with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_pdf:
pdf_path = tmp_pdf.name
# EPS to PDF with Ghostscript
subprocess.run(
['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite',
f'-sOutputFile={pdf_path}', eps_file.name],
check=True,
capture_output=True,
timeout=30
)
# PDF to SVG with Inkscape
subprocess.run(
['inkscape', pdf_path, '--export-type=svg', f'--export-filename={svg_path}'],
check=True,
capture_output=True,
timeout=30
)
os.unlink(pdf_path)
if os.path.exists(svg_path) and os.path.getsize(svg_path) > 0:
return svg_path, "Conversion successful using Ghostscript + Inkscape! Download your SVG file below."
except Exception as e:
print(f"Ghostscript method failed: {e}")
# Method 3: Fallback to Wand/ImageMagick (rasterizes to PNG first)
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_png:
png_path = tmp_png.name
with Image(filename=eps_file.name, resolution=300) as img:
img.format = 'png'
img.save(filename=png_path)
# Convert PNG to SVG using potrace
subprocess.run(
['convert', png_path, 'pnm:-'],
stdout=subprocess.PIPE,
check=True
)
with Image(filename=png_path) as img:
img.format = 'svg'
img.save(filename=svg_path)
os.unlink(png_path)
return svg_path, "Conversion successful using ImageMagick (rasterized)! Download your SVG file below."
except Exception as e:
return None, f"Error during conversion: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="EPS to SVG Converter") as app:
gr.Markdown("# EPS to SVG Converter")
gr.Markdown("Upload an EPS file to convert it to SVG format using Ghostscript and ImageMagick.")
with gr.Row():
with gr.Column():
eps_input = gr.File(
label="Upload EPS File",
file_types=[".eps"],
type="filepath"
)
convert_btn = gr.Button("Convert to SVG", variant="primary")
with gr.Column():
status_output = gr.Textbox(label="Status", interactive=False)
svg_output = gr.File(label="Download SVG")
convert_btn.click(
fn=convert_eps_to_svg,
inputs=[eps_input],
outputs=[svg_output, status_output]
)
gr.Markdown("""
### How to use:
1. Upload an EPS file using the file picker
2. Click 'Convert to SVG' button
3. Download the converted SVG file
### Requirements:
- Ghostscript (for EPS processing)
- ImageMagick (for format conversion)
- Python Wand library (ImageMagick binding)
""")
if __name__ == "__main__":
app.launch(server_name="0.0.0.0", server_port=7860)