onnew commited on
Commit
0bc52e3
·
verified ·
1 Parent(s): 0a8c67b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -66
app.py CHANGED
@@ -1,30 +1,36 @@
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,
@@ -35,55 +41,43 @@ def create_animation(frames, duration):
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()
 
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()