Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,82 +1,89 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image, ImageDraw
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
14 |
draw = ImageDraw.Draw(frame)
|
15 |
draw.rectangle(
|
16 |
-
[x
|
17 |
-
|
18 |
-
outline=color
|
19 |
-
)
|
20 |
return frame
|
21 |
|
22 |
-
|
|
|
23 |
def create_animation(frames, duration):
|
24 |
-
|
25 |
-
return None
|
26 |
-
output_gif = io.BytesIO()
|
27 |
frames[0].save(
|
28 |
-
|
29 |
-
format="GIF",
|
30 |
save_all=True,
|
31 |
append_images=frames[1:],
|
32 |
duration=duration,
|
33 |
-
loop=0
|
34 |
)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
# Função principal
|
39 |
-
def editor(frames,
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
# Interface Gradio
|
58 |
-
with gr.Blocks() as demo:
|
59 |
-
gr.Markdown("# Editor de Sprites e Animações")
|
60 |
|
61 |
with gr.Row():
|
62 |
-
|
63 |
-
color_picker = gr.ColorPicker(label="Cor")
|
64 |
-
x_input = gr.Number(label="X", value=0, precision=0)
|
65 |
-
y_input = gr.Number(label="Y", value=0, precision=0)
|
66 |
-
pixel_size = gr.Slider(label="Tamanho do Pixel", minimum=1, maximum=10, value=1)
|
67 |
-
duration = gr.Slider(label="Duração do Frame (ms)", minimum=50, maximum=500, value=100)
|
68 |
|
69 |
with gr.Row():
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
editor,
|
76 |
-
inputs=[
|
77 |
-
outputs=[
|
78 |
)
|
79 |
|
80 |
-
|
81 |
-
if __name__ == "__main__":
|
82 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
+
|
4 |
+
# Configurações gerais
|
5 |
+
GRID_SIZE = 32 # Tamanho da grade
|
6 |
+
PIXEL_SIZE = 20 # Tamanho de cada pixel no desenho
|
7 |
+
CANVAS_SIZE = GRID_SIZE * PIXEL_SIZE # Tamanho total do canvas
|
8 |
+
FRAME_COUNT = 4 # Número de frames padrão na animação
|
9 |
+
|
10 |
+
|
11 |
+
# Função para criar uma imagem em branco
|
12 |
+
def create_blank_frame():
|
13 |
+
return Image.new("RGB", (GRID_SIZE, GRID_SIZE), color="white")
|
14 |
+
|
15 |
+
|
16 |
+
# Função para desenhar no frame
|
17 |
+
def draw_pixel(frame, x, y, color):
|
18 |
draw = ImageDraw.Draw(frame)
|
19 |
draw.rectangle(
|
20 |
+
[x, y, x + 1, y + 1], fill=color
|
21 |
+
) # Cada pixel é um pequeno quadrado 1x1
|
|
|
|
|
22 |
return frame
|
23 |
|
24 |
+
|
25 |
+
# Função para converter frames em um GIF
|
26 |
def create_animation(frames, duration):
|
27 |
+
gif_path = "animation.gif"
|
|
|
|
|
28 |
frames[0].save(
|
29 |
+
gif_path,
|
|
|
30 |
save_all=True,
|
31 |
append_images=frames[1:],
|
32 |
duration=duration,
|
33 |
+
loop=0,
|
34 |
)
|
35 |
+
return gif_path
|
36 |
+
|
37 |
+
|
38 |
+
# Função principal do editor
|
39 |
+
def editor(frames, frame_index, x, y, color, duration):
|
40 |
+
frame = frames[frame_index]
|
41 |
+
frame = draw_pixel(frame, x, y, color)
|
42 |
+
frames[frame_index] = frame
|
43 |
+
|
44 |
+
gif_path = create_animation(frames, duration)
|
45 |
+
|
46 |
+
return gif_path, frames
|
47 |
+
|
48 |
+
|
49 |
+
# Inicializa frames em branco
|
50 |
+
initial_frames = [create_blank_frame() for _ in range(FRAME_COUNT)]
|
51 |
+
|
52 |
+
|
53 |
+
# Interface com Gradio
|
54 |
+
with gr.Blocks() as app:
|
55 |
+
gr.Markdown("# Editor de Sprites e Animação")
|
|
|
|
|
|
|
|
|
56 |
|
57 |
with gr.Row():
|
58 |
+
canvas = gr.Image(label="Canvas", type="pil", tool=None)
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
with gr.Row():
|
61 |
+
with gr.Column():
|
62 |
+
gr.Markdown("### Ferramentas de Desenho")
|
63 |
+
x = gr.Slider(0, GRID_SIZE - 1, step=1, label="Posição X")
|
64 |
+
y = gr.Slider(0, GRID_SIZE - 1, step=1, label="Posição Y")
|
65 |
+
color = gr.ColorPicker(label="Cor", value="#ff0000")
|
66 |
+
frame_index = gr.Slider(
|
67 |
+
0, FRAME_COUNT - 1, step=1, label="Frame Atual"
|
68 |
+
)
|
69 |
+
duration = gr.Slider(50, 500, step=10, label="Duração (ms)", value=100)
|
70 |
+
|
71 |
+
with gr.Column():
|
72 |
+
gr.Markdown("### Animação")
|
73 |
+
animation_output = gr.Image(label="Prévia da Animação")
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
update_button = gr.Button("Atualizar Frame")
|
77 |
+
save_button = gr.Button("Salvar Animação")
|
78 |
+
|
79 |
+
# Estados
|
80 |
+
frames_state = gr.State(value=initial_frames)
|
81 |
|
82 |
+
# Atualizar o frame
|
83 |
+
update_button.click(
|
84 |
editor,
|
85 |
+
inputs=[frames_state, frame_index, x, y, color, duration],
|
86 |
+
outputs=[animation_output, frames_state],
|
87 |
)
|
88 |
|
89 |
+
app.launch()
|
|
|
|