VectorFlow / app.py
cutechicken's picture
Update app.py
473a864 verified
raw
history blame
7.54 kB
import gradio as gr
import vtracer
import os
from svglib.svglib import svg2rlg
from reportlab.graphics import renderPDF
import subprocess
def convert_svg_to_ai(svg_path):
# SVG๋ฅผ PDF๋กœ ๋จผ์ € ๋ณ€ํ™˜
drawing = svg2rlg(svg_path)
pdf_path = svg_path.replace('.svg', '.pdf')
renderPDF.drawToFile(drawing, pdf_path)
# PDF๋ฅผ AI๋กœ ๋ณ€ํ™˜
ai_path = svg_path.replace('.svg', '.ai')
subprocess.run(['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite',
'-sOutputFile=' + ai_path, pdf_path])
# ์ž„์‹œ PDF ํŒŒ์ผ ์‚ญ์ œ
os.remove(pdf_path)
return ai_path
def convert_to_vector(
image,
save_svg,
save_ai,
colormode="color",
hierarchical="stacked",
mode="spline",
filter_speckle=4,
color_precision=6,
layer_difference=16,
corner_threshold=60,
length_threshold=4.0,
max_iterations=10,
splice_threshold=45,
path_precision=3
):
if not (save_svg or save_ai):
return None, None, None # ๋‘˜ ๋‹ค ์„ ํƒ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
input_path = "temp_input.jpg"
svg_path = "svg_output.svg"
outputs = []
preview = None
# ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ
image.save(input_path)
# VTracer๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€๋ฅผ SVG๋กœ ๋ณ€ํ™˜
vtracer.convert_image_to_svg_py(
input_path,
svg_path,
colormode=colormode,
hierarchical=hierarchical,
mode=mode,
filter_speckle=int(filter_speckle),
color_precision=int(color_precision),
layer_difference=int(layer_difference),
corner_threshold=int(corner_threshold),
length_threshold=float(length_threshold),
max_iterations=int(max_iterations),
splice_threshold=int(splice_threshold),
path_precision=int(path_precision)
)
# SVG ํŒŒ์ผ ์ฒ˜๋ฆฌ
if save_svg:
with open(svg_path, "r") as f:
svg_content = f.read()
preview = gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>')
outputs.append(svg_path)
# AI ํŒŒ์ผ ์ฒ˜๋ฆฌ
if save_ai:
ai_path = convert_svg_to_ai(svg_path)
outputs.append(ai_path)
if not save_svg: # SVG๊ฐ€ ์„ ํƒ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
os.remove(svg_path)
return preview, outputs[0] if outputs else None, outputs[1] if len(outputs) > 1 else None
css = """
#col-container {
margin: 0 auto;
max-width: 960px;
}
.generate-btn {
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
border: none !important;
color: white !important;
}
.generate-btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
"""
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
with gr.Blocks(css=css) as app:
with gr.Column(elem_id="col-container"):
gr.HTML("""
<div style="text-align: center;">
<h2>Image to Vector Converter โšก</h2>
<p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG, AI).</p>
</div>
""")
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Upload Image")
with gr.Row():
save_svg = gr.Checkbox(value=True, label="Save as SVG")
save_ai = gr.Checkbox(value=False, label="Save as AI")
with gr.Accordion("Advanced Settings", open=False):
with gr.Accordion("Clustering", open=False):
colormode = gr.Radio([("COLOR","color"),("B/W", "binary")], value="color", label="Color Mode", show_label=False)
filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle", info="Cleaner")
color_precision = gr.Slider(1, 8, value=6, step=1, label="Color Precision", info="More accurate")
layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step", info="Less layers")
hierarchical = gr.Radio([("STACKED","stacked"), ("CUTOUT","cutout")], value="stacked", label="Hierarchical Mode",show_label=False)
with gr.Accordion("Curve Fitting", open=False):
mode = gr.Radio([("SPLINE","spline"),("POLYGON", "polygon"), ("PIXEL","none")], value="spline", label="Mode", show_label=False)
corner_threshold = gr.Slider(0, 180, value=60, step=1, label="Corner Threshold", info="Smoother")
length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.1, label="Segment Length", info="More coarse")
splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold", info="Less accurate")
max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations", visible=False)
path_precision = gr.Slider(1, 10, value=3, step=1, label="Path Precision", visible=False)
output_text = gr.Textbox(label="Selected Mode", visible=False)
with gr.Row():
clear_button = gr.Button("Clear")
convert_button = gr.Button("โœจ Convert", variant='primary', elem_classes=["generate-btn"])
with gr.Column():
preview = gr.HTML(label="Preview")
svg_output = gr.File(label="SVG Output", visible=True)
ai_output = gr.File(label="AI Output", visible=True)
examples = [
"examples/11.jpg",
"examples/02.jpg",
"examples/03.jpg",
]
gr.Examples(
examples=examples,
fn=convert_to_vector,
inputs=[
image_input,
save_svg,
save_ai
],
outputs=[preview, svg_output, ai_output],
cache_examples=False,
run_on_click=True
)
# ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
def clear_inputs():
return [None] * 12
def update_output_visibility(save_svg, save_ai):
return gr.update(visible=save_svg), gr.update(visible=save_ai)
# ์ฒดํฌ๋ฐ•์Šค ์ƒํƒœ์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ ํŒŒ์ผ ์ปดํฌ๋„ŒํŠธ ํ‘œ์‹œ/์ˆจ๊น€
save_svg.change(
update_output_visibility,
inputs=[save_svg, save_ai],
outputs=[svg_output, ai_output]
)
save_ai.change(
update_output_visibility,
inputs=[save_svg, save_ai],
outputs=[svg_output, ai_output]
)
# ๋ณ€ํ™˜ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
convert_button.click(
convert_to_vector,
inputs=[
image_input,
save_svg,
save_ai,
colormode,
hierarchical,
mode,
filter_speckle,
color_precision,
layer_difference,
corner_threshold,
length_threshold,
max_iterations,
splice_threshold,
path_precision
],
outputs=[preview, svg_output, ai_output]
)
# Clear ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
clear_button.click(
clear_inputs,
outputs=[
image_input,
colormode,
hierarchical,
mode,
filter_speckle,
color_precision,
layer_difference,
corner_threshold,
length_threshold,
max_iterations,
splice_threshold,
path_precision
]
)
app.launch(debug=True)