File size: 4,357 Bytes
d105ffe
 
 
 
53d899a
d105ffe
 
 
53d899a
d105ffe
 
 
 
 
 
 
 
 
53d899a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d105ffe
 
 
53d899a
d105ffe
53d899a
d105ffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)