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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -46
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
- # SVG๋ฅผ PDF๋กœ ๋จผ์ € ๋ณ€ํ™˜
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
- # ์ž„์‹œ PDF ํŒŒ์ผ ์‚ญ์ œ
20
- os.remove(pdf_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- image.save(input_path)
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
- # SVG ํŒŒ์ผ ์ฒ˜๋ฆฌ
68
- if save_svg:
69
- with open(svg_path, "r") as f:
70
- svg_content = f.read()
71
- preview = gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>')
72
- outputs.append(svg_path)
73
-
74
- # AI ํŒŒ์ผ ์ฒ˜๋ฆฌ
75
- if save_ai:
76
- ai_path = convert_svg_to_ai(svg_path)
77
- outputs.append(ai_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
- if not save_svg: # SVG๊ฐ€ ์„ ํƒ๋˜์ง€ ์•Š์•˜๋‹ค๋ฉด ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
80
- os.remove(svg_path)
 
81
 
82
- return preview, outputs[0] if outputs else None, outputs[1] if len(outputs) > 1 else None
 
 
 
 
 
 
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 {