Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,36 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageDraw
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
|
16 |
-
# Função para
|
17 |
-
def
|
18 |
-
|
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 |
-
#
|
26 |
-
|
27 |
-
gif_path = "animation.gif"
|
28 |
frames[0].save(
|
29 |
gif_path,
|
30 |
save_all=True,
|
@@ -35,55 +41,43 @@ def create_animation(frames, duration):
|
|
35 |
return gif_path
|
36 |
|
37 |
|
38 |
-
#
|
39 |
-
def
|
40 |
-
|
41 |
-
|
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("#
|
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 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
)
|
88 |
|
89 |
app.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageDraw
|
3 |
+
import os
|
4 |
+
|
5 |
+
|
6 |
+
# Função para criar frames a partir de prompts (exemplo usando texto sobre imagens)
|
7 |
+
def generate_frame(prompt, frame_number, width=256, height=256):
|
8 |
+
# Cria uma imagem em branco
|
9 |
+
img = Image.new("RGB", (width, height), color="white")
|
10 |
+
draw = ImageDraw.Draw(img)
|
11 |
+
|
12 |
+
# Adiciona o texto do prompt no centro
|
13 |
+
text = f"{prompt} ({frame_number})"
|
14 |
+
text_width, text_height = draw.textsize(text)
|
15 |
+
draw.text(
|
16 |
+
((width - text_width) / 2, (height - text_height) / 2),
|
17 |
+
text,
|
18 |
+
fill="black",
|
19 |
+
)
|
20 |
+
return img
|
21 |
|
22 |
|
23 |
+
# Função para criar uma animação (GIF)
|
24 |
+
def create_animation_from_prompts(prompt, frame_count=10, duration=100):
|
25 |
+
frames = []
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Gera os frames com base no prompt
|
28 |
+
for i in range(frame_count):
|
29 |
+
frame = generate_frame(prompt, i + 1)
|
30 |
+
frames.append(frame)
|
31 |
|
32 |
+
# Salva a animação como GIF
|
33 |
+
gif_path = "output.gif"
|
|
|
34 |
frames[0].save(
|
35 |
gif_path,
|
36 |
save_all=True,
|
|
|
41 |
return gif_path
|
42 |
|
43 |
|
44 |
+
# Interface Gradio
|
45 |
+
def animate_sprite(prompt, frame_count, duration):
|
46 |
+
gif_path = create_animation_from_prompts(prompt, frame_count, duration)
|
47 |
+
return gif_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
|
|
|
50 |
with gr.Blocks() as app:
|
51 |
+
gr.Markdown("# Gerador de Animações com Prompt de Texto")
|
52 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
with gr.Row():
|
54 |
+
prompt = gr.Textbox(
|
55 |
+
label="Descrição do Sprite",
|
56 |
+
placeholder="Descreva o sprite ou a animação que deseja criar",
|
57 |
+
)
|
58 |
+
frame_count = gr.Slider(
|
59 |
+
label="Número de Frames",
|
60 |
+
minimum=1,
|
61 |
+
maximum=30,
|
62 |
+
value=10,
|
63 |
+
step=1,
|
64 |
+
)
|
65 |
+
duration = gr.Slider(
|
66 |
+
label="Duração do Frame (ms)",
|
67 |
+
minimum=50,
|
68 |
+
maximum=500,
|
69 |
+
value=100,
|
70 |
+
step=10,
|
71 |
+
)
|
72 |
+
|
73 |
+
animation_output = gr.Image(label="Animação Gerada")
|
74 |
+
|
75 |
+
generate_button = gr.Button("Gerar Animação")
|
76 |
+
|
77 |
+
generate_button.click(
|
78 |
+
animate_sprite,
|
79 |
+
inputs=[prompt, frame_count, duration],
|
80 |
+
outputs=animation_output,
|
81 |
)
|
82 |
|
83 |
app.launch()
|