Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,56 @@
|
|
1 |
import gradio as gr
|
2 |
import vtracer
|
3 |
import os
|
4 |
-
from svglib.svglib import svg2rlg
|
5 |
-
from reportlab.graphics import renderPDF
|
6 |
-
import subprocess
|
7 |
|
8 |
def convert_svg_to_ai(svg_path):
|
9 |
-
|
10 |
-
drawing = svg2rlg(svg_path)
|
11 |
-
pdf_path = svg_path.replace('.svg', '.pdf')
|
12 |
-
renderPDF.drawToFile(drawing, pdf_path)
|
13 |
-
|
14 |
-
# PDF๋ฅผ AI๋ก ๋ณํ
|
15 |
ai_path = svg_path.replace('.svg', '.ai')
|
16 |
-
subprocess.run(['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite',
|
17 |
-
'-sOutputFile=' + ai_path, pdf_path])
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
return ai_path
|
22 |
|
23 |
def convert_to_vector(
|
@@ -39,47 +72,63 @@ def convert_to_vector(
|
|
39 |
if not (save_svg or save_ai):
|
40 |
return None, None, None # ๋ ๋ค ์ ํ๋์ง ์์ ๊ฒฝ์ฐ
|
41 |
|
|
|
|
|
|
|
42 |
input_path = "temp_input.jpg"
|
43 |
svg_path = "svg_output.svg"
|
44 |
outputs = []
|
45 |
preview = None
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
# VTracer๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ SVG๋ก ๋ณํ
|
51 |
-
vtracer.convert_image_to_svg_py(
|
52 |
-
input_path,
|
53 |
-
svg_path,
|
54 |
-
colormode=colormode,
|
55 |
-
hierarchical=hierarchical,
|
56 |
-
mode=mode,
|
57 |
-
filter_speckle=int(filter_speckle),
|
58 |
-
color_precision=int(color_precision),
|
59 |
-
layer_difference=int(layer_difference),
|
60 |
-
corner_threshold=int(corner_threshold),
|
61 |
-
length_threshold=float(length_threshold),
|
62 |
-
max_iterations=int(max_iterations),
|
63 |
-
splice_threshold=int(splice_threshold),
|
64 |
-
path_precision=int(path_precision)
|
65 |
-
)
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
|
79 |
-
|
80 |
-
|
|
|
81 |
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
css = """
|
85 |
#col-container {
|
|
|
1 |
import gradio as gr
|
2 |
import vtracer
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
def convert_svg_to_ai(svg_path):
|
6 |
+
"""SVG๋ฅผ AI ํ์ผ๋ก ๋ณํ"""
|
|
|
|
|
|
|
|
|
|
|
7 |
ai_path = svg_path.replace('.svg', '.ai')
|
|
|
|
|
8 |
|
9 |
+
# SVG ํ์ผ ์ฝ๊ธฐ
|
10 |
+
with open(svg_path, 'r') as svg_file:
|
11 |
+
svg_content = svg_file.read()
|
12 |
+
|
13 |
+
# AI ํ์ผ ํค๋
|
14 |
+
ai_header = """%!PS-Adobe-3.0
|
15 |
+
%%Creator: Adobe Illustrator(TM) SVG Converter
|
16 |
+
%%AI8_CreatorVersion: 24.0.0
|
17 |
+
%%For: SVG to AI Converter
|
18 |
+
%%Title: Generated AI File
|
19 |
+
%%CreationDate: %(date)s
|
20 |
+
%%BoundingBox: 0 0 800 600
|
21 |
+
%%HiResBoundingBox: 0 0 800 600
|
22 |
+
%%DocumentData: Clean7Bit
|
23 |
+
%%LanguageLevel: 2
|
24 |
+
%%DocumentNeededResources: procset Adobe_packedarray 2.0 0
|
25 |
+
%%+ procset Adobe_cmykcolor 1.1 0
|
26 |
+
%%+ procset Adobe_cshow 1.1 0
|
27 |
+
%%+ procset Adobe_customcolor 1.0 0
|
28 |
+
%%+ procset Adobe_typography_AI5 1.0 1
|
29 |
+
%%+ procset Adobe_pattern_AI3 1.0 1
|
30 |
+
%%+ procset Adobe_Illustrator_AI3 1.0 1
|
31 |
+
%%EndComments
|
32 |
+
%%BeginProlog
|
33 |
+
"""
|
34 |
+
|
35 |
+
# AI ํ์ผ ์์ฑ
|
36 |
+
with open(ai_path, 'w', encoding='utf-8') as ai_file:
|
37 |
+
# ํค๋ ์์ฑ
|
38 |
+
ai_file.write(ai_header)
|
39 |
+
|
40 |
+
# SVG ๋ด์ฉ์ AI ํ์ผ์ ํฌํจ
|
41 |
+
ai_file.write("\n%%BeginDocument\n")
|
42 |
+
ai_file.write("/SVGContent\n<<\n")
|
43 |
+
ai_file.write("/Type /SVG\n")
|
44 |
+
ai_file.write("/Version 1.1\n")
|
45 |
+
ai_file.write("/Content [\n")
|
46 |
+
ai_file.write(svg_content)
|
47 |
+
ai_file.write("\n]\n")
|
48 |
+
ai_file.write(">>\ndef\n")
|
49 |
+
ai_file.write("\n%%EndDocument\n")
|
50 |
+
|
51 |
+
# ํ์ผ ์ข
๋ฃ
|
52 |
+
ai_file.write("\n%%Trailer\n%%EOF\n")
|
53 |
+
|
54 |
return ai_path
|
55 |
|
56 |
def convert_to_vector(
|
|
|
72 |
if not (save_svg or save_ai):
|
73 |
return None, None, None # ๋ ๋ค ์ ํ๋์ง ์์ ๊ฒฝ์ฐ
|
74 |
|
75 |
+
if image is None:
|
76 |
+
return None, None, None
|
77 |
+
|
78 |
input_path = "temp_input.jpg"
|
79 |
svg_path = "svg_output.svg"
|
80 |
outputs = []
|
81 |
preview = None
|
82 |
|
83 |
+
try:
|
84 |
+
# ์
๋ ฅ ์ด๋ฏธ์ง๋ฅผ ์์ ํ์ผ๋ก ์ ์ฅ
|
85 |
+
image.save(input_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
+
# VTracer๋ฅผ ์ฌ์ฉํ์ฌ ์ด๋ฏธ์ง๋ฅผ SVG๋ก ๋ณํ
|
88 |
+
vtracer.convert_image_to_svg_py(
|
89 |
+
input_path,
|
90 |
+
svg_path,
|
91 |
+
colormode=colormode,
|
92 |
+
hierarchical=hierarchical,
|
93 |
+
mode=mode,
|
94 |
+
filter_speckle=int(filter_speckle),
|
95 |
+
color_precision=int(color_precision),
|
96 |
+
layer_difference=int(layer_difference),
|
97 |
+
corner_threshold=int(corner_threshold),
|
98 |
+
length_threshold=float(length_threshold),
|
99 |
+
max_iterations=int(max_iterations),
|
100 |
+
splice_threshold=int(splice_threshold),
|
101 |
+
path_precision=int(path_precision)
|
102 |
+
)
|
103 |
+
|
104 |
+
# SVG ํ์ผ ์ฒ๋ฆฌ
|
105 |
+
if save_svg:
|
106 |
+
with open(svg_path, "r", encoding='utf-8') as f:
|
107 |
+
svg_content = f.read()
|
108 |
+
preview = gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>')
|
109 |
+
outputs.append(svg_path)
|
110 |
+
|
111 |
+
# AI ํ์ผ ์ฒ๋ฆฌ
|
112 |
+
if save_ai:
|
113 |
+
ai_path = convert_svg_to_ai(svg_path)
|
114 |
+
outputs.append(ai_path)
|
115 |
+
|
116 |
+
if not save_svg: # SVG๊ฐ ์ ํ๋์ง ์์๋ค๋ฉด ์์ ํ์ผ ์ญ์
|
117 |
+
os.remove(svg_path)
|
118 |
+
|
119 |
+
return preview, outputs[0] if outputs else None, outputs[1] if len(outputs) > 1 else None
|
120 |
|
121 |
+
except Exception as e:
|
122 |
+
print(f"Error during conversion: {str(e)}")
|
123 |
+
return None, None, None
|
124 |
|
125 |
+
finally:
|
126 |
+
# ์์ ํ์ผ ์ ๋ฆฌ
|
127 |
+
if os.path.exists(input_path):
|
128 |
+
try:
|
129 |
+
os.remove(input_path)
|
130 |
+
except:
|
131 |
+
pass
|
132 |
|
133 |
css = """
|
134 |
#col-container {
|