import gradio as gr
import vtracer
import os
def create_preview_html(svg_path, image_width, image_height):
"""SVG를 HTML 미리보기로 변환"""
try:
with open(svg_path, 'r', encoding='utf-8') as f:
svg_content = f.read()
# SVG를 HTML로 감싸서 반환하되, 크기를 제어
preview_html = f'''
'''
return preview_html
except Exception as e:
print(f"미리보기 생성 실패: {str(e)}")
return None
def convert_svg_to_ai(svg_path, image_width, image_height):
"""SVG를 AI 파일로 변환"""
ai_path = svg_path.replace('.svg', '.ai')
# SVG 파일 읽기
with open(svg_path, 'r') as svg_file:
svg_content = svg_file.read()
# AI 파일 헤더
ai_header = """%!PS-Adobe-3.0
%%Creator: Adobe Illustrator(TM) SVG Converter
%%AI8_CreatorVersion: 24.0.0
%%For: VectorFlow
%%Title: Generated AI File
%%CreationDate: %(date)s
%%BoundingBox: 0 0 800 600
%%HiResBoundingBox: 0 0 800 600
%%DocumentData: Clean7Bit
%%LanguageLevel: 2
%%DocumentNeededResources: procset Adobe_packedarray 2.0 0
%%+ procset Adobe_cmykcolor 1.1 0
%%+ procset Adobe_cshow 1.1 0
%%+ procset Adobe_customcolor 1.0 0
%%+ procset Adobe_typography_AI5 1.0 1
%%+ procset Adobe_pattern_AI3 1.0 1
%%+ procset Adobe_Illustrator_AI3 1.0 1
%%EndComments
%%BeginProlog
"""
# AI 파일 생성
with open(ai_path, 'w', encoding='utf-8') as ai_file:
ai_file.write(ai_header)
ai_file.write("\n%%BeginDocument\n")
ai_file.write("/SVGContent\n<<\n")
ai_file.write("/Type /SVG\n")
ai_file.write("/Version 1.1\n")
ai_file.write("/Content [\n")
ai_file.write(svg_content)
ai_file.write("\n]\n")
ai_file.write(">>\ndef\n")
ai_file.write("\n%%EndDocument\n")
ai_file.write("\n%%Trailer\n%%EOF\n")
# HTML 미리보기 생성
preview_html = create_preview_html(svg_path, image_width, image_height)
return ai_path, preview_html
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, None # Preview, SVG output, AI output, AI preview
if image is None:
return None, None, None, None
input_path = "temp_input.jpg"
svg_path = "svg_output.svg"
outputs = []
preview = None
ai_preview = None
try:
# 입력 이미지를 임시 파일로 저장
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:
preview = gr.HTML(create_preview_html(svg_path, image.width, image.height))
outputs.append(svg_path)
# AI 파일 처리
if save_ai:
ai_path, ai_preview_html = convert_svg_to_ai(svg_path, image.width, image.height)
outputs.append(ai_path)
ai_preview = gr.HTML(ai_preview_html)
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,
ai_preview
)
except Exception as e:
print(f"Error during conversion: {str(e)}")
return None, None, None, None
finally:
# 임시 파일 정리
if os.path.exists(input_path):
try:
os.remove(input_path)
except:
pass
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);
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 40px;
}
.header h1 {
font-size: 2.5em;
color: #2c3e50;
margin-bottom: 10px;
}
.header p {
color: #7f8c8d;
font-size: 1.2em;
}
"""
# Gradio 인터페이스 정의
with gr.Blocks(css=css) as app:
with gr.Column(elem_id="col-container"):
gr.HTML("""
VectorFlow ⚡
Transform your images into professional vector graphics