Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,68 +4,65 @@ from PIL import Image
|
|
4 |
import vtracer
|
5 |
import tempfile
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
10 |
-
"""
|
11 |
|
12 |
-
#
|
13 |
img_byte_array = io.BytesIO()
|
14 |
-
|
15 |
img_bytes = img_byte_array.getvalue()
|
16 |
|
17 |
-
#
|
18 |
svg_str = vtracer.convert_raw_image_to_svg(
|
19 |
img_bytes,
|
20 |
img_format='png',
|
21 |
-
colormode=
|
22 |
-
hierarchical=
|
23 |
-
mode=
|
24 |
-
filter_speckle=int(
|
25 |
-
color_precision=int(
|
26 |
-
layer_difference=int(
|
27 |
-
corner_threshold=int(
|
28 |
-
length_threshold=float(
|
29 |
-
max_iterations=int(
|
30 |
-
splice_threshold=int(
|
31 |
-
path_precision=int(
|
32 |
)
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
|
39 |
-
return gr.HTML(f'<svg viewBox="0 0 {
|
40 |
|
41 |
-
# Gradio
|
42 |
iface = gr.Blocks()
|
43 |
|
44 |
with iface:
|
45 |
|
46 |
gr.Interface(
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
description="Upload an image and customize the conversion parameters as needed.<br><h2>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2>",
|
68 |
-
)
|
69 |
|
70 |
-
|
71 |
-
iface.launch()
|
|
|
4 |
import vtracer
|
5 |
import tempfile
|
6 |
|
7 |
+
def converter_imagem(imagem, modo_cor, hierarquico, modo, filtro_ruido,
|
8 |
+
precisao_cor, diferenca_camada, limite_canto,
|
9 |
+
limite_comprimento, max_iteracoes, limite_emenda, precisao_caminho):
|
10 |
+
"""Converte uma imagem para SVG usando vtracer com parâmetros personalizáveis."""
|
11 |
|
12 |
+
# Converter a imagem do Gradio para bytes para compatibilidade com o vtracer
|
13 |
img_byte_array = io.BytesIO()
|
14 |
+
imagem.save(img_byte_array, format='PNG')
|
15 |
img_bytes = img_byte_array.getvalue()
|
16 |
|
17 |
+
# Realizar a conversão
|
18 |
svg_str = vtracer.convert_raw_image_to_svg(
|
19 |
img_bytes,
|
20 |
img_format='png',
|
21 |
+
colormode=modo_cor.lower(),
|
22 |
+
hierarchical=hierarquico.lower(),
|
23 |
+
mode=modo.lower(),
|
24 |
+
filter_speckle=int(filtro_ruido),
|
25 |
+
color_precision=int(precisao_cor),
|
26 |
+
layer_difference=int(diferenca_camada),
|
27 |
+
corner_threshold=int(limite_canto),
|
28 |
+
length_threshold=float(limite_comprimento),
|
29 |
+
max_iterations=int(max_iteracoes),
|
30 |
+
splice_threshold=int(limite_emenda),
|
31 |
+
path_precision=int(precisao_caminho)
|
32 |
)
|
33 |
|
34 |
+
# Salvar o SVG em um arquivo temporário
|
35 |
+
arquivo_temporario = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
|
36 |
+
arquivo_temporario.write(svg_str.encode('utf-8'))
|
37 |
+
arquivo_temporario.close()
|
38 |
|
39 |
+
return gr.HTML(f'<svg viewBox="0 0 {imagem.width} {imagem.height}">{svg_str}</svg>'), arquivo_temporario.name
|
40 |
|
41 |
+
# Interface Gradio
|
42 |
iface = gr.Blocks()
|
43 |
|
44 |
with iface:
|
45 |
|
46 |
gr.Interface(
|
47 |
+
fn=converter_imagem,
|
48 |
+
inputs=[
|
49 |
+
gr.Image(type="pil", label="Enviar Imagem"),
|
50 |
+
gr.Radio(choices=["Colorido", "Binário"], value="Colorido", label="Modo de Cor"),
|
51 |
+
gr.Radio(choices=["Empilhado", "Recortado"], value="Empilhado", label="Hierárquico"),
|
52 |
+
gr.Radio(choices=["Spline", "Polígono", "Nenhum"], value="Spline", label="Modo"),
|
53 |
+
gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filtro de Ruído"),
|
54 |
+
gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Precisão de Cor"),
|
55 |
+
gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Diferença de Camada"),
|
56 |
+
gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Limite de Canto"),
|
57 |
+
gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Limite de Comprimento"),
|
58 |
+
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Máx. Iterações"),
|
59 |
+
gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Limite de Emenda"),
|
60 |
+
gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Precisão do Caminho")
|
61 |
+
],
|
62 |
+
outputs=[
|
63 |
+
gr.HTML(label="Saída SVG"),
|
64 |
+
gr.File(label="Baixar SVG")
|
65 |
+
]
|
66 |
+
)
|
|
|
|
|
67 |
|
68 |
+
iface.launch()
|
|