cutechicken commited on
Commit
b067401
ยท
verified ยท
1 Parent(s): 9001c78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -24
app.py CHANGED
@@ -1,6 +1,19 @@
1
  import gradio as gr
2
  import vtracer
3
  import os
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  def convert_svg_to_ai(svg_path):
6
  """SVG๋ฅผ AI ํŒŒ์ผ๋กœ ๋ณ€ํ™˜"""
@@ -14,7 +27,7 @@ def convert_svg_to_ai(svg_path):
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
@@ -34,10 +47,7 @@ def convert_svg_to_ai(svg_path):
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")
@@ -47,11 +57,12 @@ def convert_svg_to_ai(svg_path):
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(
57
  image,
@@ -70,15 +81,16 @@ def convert_to_vector(
70
  path_precision=3
71
  ):
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
  # ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ
@@ -110,17 +122,24 @@ def convert_to_vector(
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
  # ์ž„์‹œ ํŒŒ์ผ ์ •๋ฆฌ
@@ -144,15 +163,34 @@ css = """
144
  transform: translateY(-2px);
145
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
146
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  """
148
 
149
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
150
  with gr.Blocks(css=css) as app:
151
  with gr.Column(elem_id="col-container"):
152
  gr.HTML("""
153
- <div style="text-align: center;">
154
- <h2>Image to Vector Converter โšก</h2>
155
- <p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG, AI).</p>
 
156
  </div>
157
  """)
158
  with gr.Row():
@@ -181,9 +219,13 @@ with gr.Blocks(css=css) as app:
181
  convert_button = gr.Button("โœจ Convert", variant='primary', elem_classes=["generate-btn"])
182
 
183
  with gr.Column():
184
- preview = gr.HTML(label="Preview")
185
- svg_output = gr.File(label="SVG Output", visible=True)
186
- ai_output = gr.File(label="AI Output", visible=True)
 
 
 
 
187
 
188
  examples = [
189
  ["examples/11.jpg", True, False],
@@ -199,7 +241,7 @@ with gr.Blocks(css=css) as app:
199
  save_svg,
200
  save_ai
201
  ],
202
- outputs=[preview, svg_output, ai_output],
203
  cache_examples=False,
204
  run_on_click=True
205
  )
@@ -209,18 +251,23 @@ with gr.Blocks(css=css) as app:
209
  return [None] * 12
210
 
211
  def update_output_visibility(save_svg, save_ai):
212
- return gr.update(visible=save_svg), gr.update(visible=save_ai)
 
 
 
 
 
213
 
214
  # ์ฒดํฌ๋ฐ•์Šค ์ƒํƒœ์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ ํŒŒ์ผ ์ปดํฌ๋„ŒํŠธ ํ‘œ์‹œ/์ˆจ๊น€
215
  save_svg.change(
216
  update_output_visibility,
217
  inputs=[save_svg, save_ai],
218
- outputs=[svg_output, ai_output]
219
  )
220
  save_ai.change(
221
  update_output_visibility,
222
  inputs=[save_svg, save_ai],
223
- outputs=[svg_output, ai_output]
224
  )
225
 
226
  # ๋ณ€ํ™˜ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
@@ -242,7 +289,7 @@ with gr.Blocks(css=css) as app:
242
  splice_threshold,
243
  path_precision
244
  ],
245
- outputs=[preview, svg_output, ai_output]
246
  )
247
 
248
  # Clear ๋ฒ„ํŠผ ์ด๋ฒคํŠธ
 
1
  import gradio as gr
2
  import vtracer
3
  import os
4
+ from svglib.svglib import svg2rlg
5
+ from reportlab.graphics import renderPDF
6
+
7
+ def create_pdf_preview(svg_path):
8
+ """SVG๋ฅผ PDF๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฏธ๋ฆฌ๋ณด๊ธฐ์šฉ PDF ์ƒ์„ฑ"""
9
+ try:
10
+ drawing = svg2rlg(svg_path)
11
+ pdf_path = svg_path.replace('.svg', '_preview.pdf')
12
+ renderPDF.drawToFile(drawing, pdf_path)
13
+ return pdf_path
14
+ except Exception as e:
15
+ print(f"PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ƒ์„ฑ ์‹คํŒจ: {str(e)}")
16
+ return None
17
 
18
  def convert_svg_to_ai(svg_path):
19
  """SVG๋ฅผ AI ํŒŒ์ผ๋กœ ๋ณ€ํ™˜"""
 
27
  ai_header = """%!PS-Adobe-3.0
28
  %%Creator: Adobe Illustrator(TM) SVG Converter
29
  %%AI8_CreatorVersion: 24.0.0
30
+ %%For: VectorFlow
31
  %%Title: Generated AI File
32
  %%CreationDate: %(date)s
33
  %%BoundingBox: 0 0 800 600
 
47
 
48
  # AI ํŒŒ์ผ ์ƒ์„ฑ
49
  with open(ai_path, 'w', encoding='utf-8') as ai_file:
 
50
  ai_file.write(ai_header)
 
 
51
  ai_file.write("\n%%BeginDocument\n")
52
  ai_file.write("/SVGContent\n<<\n")
53
  ai_file.write("/Type /SVG\n")
 
57
  ai_file.write("\n]\n")
58
  ai_file.write(">>\ndef\n")
59
  ai_file.write("\n%%EndDocument\n")
 
 
60
  ai_file.write("\n%%Trailer\n%%EOF\n")
61
 
62
+ # PDF ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ƒ์„ฑ
63
+ pdf_preview = create_pdf_preview(svg_path)
64
+
65
+ return ai_path, pdf_preview
66
 
67
  def convert_to_vector(
68
  image,
 
81
  path_precision=3
82
  ):
83
  if not (save_svg or save_ai):
84
+ return None, None, None, None # Preview, SVG output, AI output, AI preview
85
+
86
  if image is None:
87
+ return None, None, None, None
88
 
89
  input_path = "temp_input.jpg"
90
  svg_path = "svg_output.svg"
91
  outputs = []
92
  preview = None
93
+ ai_preview = None
94
 
95
  try:
96
  # ์ž…๋ ฅ ์ด๋ฏธ์ง€๋ฅผ ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ
 
122
 
123
  # AI ํŒŒ์ผ ์ฒ˜๋ฆฌ
124
  if save_ai:
125
+ ai_path, pdf_preview = convert_svg_to_ai(svg_path)
126
  outputs.append(ai_path)
127
+ if pdf_preview:
128
+ ai_preview = pdf_preview
129
 
130
  if not save_svg: # SVG๊ฐ€ ์„ ํƒ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
131
  os.remove(svg_path)
132
 
133
+ return (
134
+ preview,
135
+ outputs[0] if outputs else None,
136
+ outputs[1] if len(outputs) > 1 else None,
137
+ ai_preview
138
+ )
139
 
140
  except Exception as e:
141
  print(f"Error during conversion: {str(e)}")
142
+ return None, None, None, None
143
 
144
  finally:
145
  # ์ž„์‹œ ํŒŒ์ผ ์ •๋ฆฌ
 
163
  transform: translateY(-2px);
164
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
165
  }
166
+ .container {
167
+ max-width: 1200px;
168
+ margin: 0 auto;
169
+ padding: 20px;
170
+ }
171
+ .header {
172
+ text-align: center;
173
+ margin-bottom: 40px;
174
+ }
175
+ .header h1 {
176
+ font-size: 2.5em;
177
+ color: #2c3e50;
178
+ margin-bottom: 10px;
179
+ }
180
+ .header p {
181
+ color: #7f8c8d;
182
+ font-size: 1.2em;
183
+ }
184
  """
185
 
186
  # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜
187
  with gr.Blocks(css=css) as app:
188
  with gr.Column(elem_id="col-container"):
189
  gr.HTML("""
190
+ <div class="header">
191
+ <h1>VectorFlow โšก</h1>
192
+ <p>Transform your images into professional vector graphics</p>
193
+ <p style="font-size: 0.9em; color: #95a5a6;">Supports JPG, PNG, WEBP โ†’ SVG, AI</p>
194
  </div>
195
  """)
196
  with gr.Row():
 
219
  convert_button = gr.Button("โœจ Convert", variant='primary', elem_classes=["generate-btn"])
220
 
221
  with gr.Column():
222
+ with gr.Tabs():
223
+ with gr.Tab("SVG Preview"):
224
+ preview = gr.HTML(label="SVG Preview")
225
+ svg_output = gr.File(label="Download SVG", visible=True)
226
+ with gr.Tab("AI Preview"):
227
+ ai_preview = gr.PDF(label="AI Preview")
228
+ ai_output = gr.File(label="Download AI", visible=True)
229
 
230
  examples = [
231
  ["examples/11.jpg", True, False],
 
241
  save_svg,
242
  save_ai
243
  ],
244
+ outputs=[preview, svg_output, ai_output, ai_preview],
245
  cache_examples=False,
246
  run_on_click=True
247
  )
 
251
  return [None] * 12
252
 
253
  def update_output_visibility(save_svg, save_ai):
254
+ return (
255
+ gr.update(visible=save_svg),
256
+ gr.update(visible=save_ai),
257
+ gr.update(visible=save_svg),
258
+ gr.update(visible=save_ai)
259
+ )
260
 
261
  # ์ฒดํฌ๋ฐ•์Šค ์ƒํƒœ์— ๋”ฐ๋ฅธ ์ถœ๋ ฅ ํŒŒ์ผ ์ปดํฌ๋„ŒํŠธ ํ‘œ์‹œ/์ˆจ๊น€
262
  save_svg.change(
263
  update_output_visibility,
264
  inputs=[save_svg, save_ai],
265
+ outputs=[preview, ai_preview, svg_output, ai_output]
266
  )
267
  save_ai.change(
268
  update_output_visibility,
269
  inputs=[save_svg, save_ai],
270
+ outputs=[preview, ai_preview, svg_output, ai_output]
271
  )
272
 
273
  # ๋ณ€ํ™˜ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
 
289
  splice_threshold,
290
  path_precision
291
  ],
292
+ outputs=[preview, svg_output, ai_output, ai_preview]
293
  )
294
 
295
  # Clear ๋ฒ„ํŠผ ์ด๋ฒคํŠธ