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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -63
app.py CHANGED
@@ -1,82 +1,89 @@
1
  import gradio as gr
2
- from PIL import Image, ImageDraw, ImageSequence
3
- import io
4
-
5
- # Função para criar ou editar frames
6
- def edit_frame(frame, color, x, y, pixel_size):
7
- if frame is None:
8
- # Cria um frame vazio
9
- frame = Image.new("RGBA", (64, 64), (255, 255, 255, 0))
10
- else:
11
- frame = frame.convert("RGBA")
12
-
13
- # Desenha o pixel
 
 
 
 
14
  draw = ImageDraw.Draw(frame)
15
  draw.rectangle(
16
- [x * pixel_size, y * pixel_size, (x + 1) * pixel_size - 1, (y + 1) * pixel_size - 1],
17
- fill=color,
18
- outline=color
19
- )
20
  return frame
21
 
22
- # Função para criar uma animação GIF
 
23
  def create_animation(frames, duration):
24
- if not frames:
25
- return None
26
- output_gif = io.BytesIO()
27
  frames[0].save(
28
- output_gif,
29
- format="GIF",
30
  save_all=True,
31
  append_images=frames[1:],
32
  duration=duration,
33
- loop=0
34
  )
35
- output_gif.seek(0)
36
- return output_gif
37
-
38
- # Função principal para o Gradio
39
- def editor(frames, color, x, y, pixel_size, duration):
40
- updated_frames = []
41
- for frame_data in frames:
42
- if frame_data is None:
43
- updated_frames.append(None)
44
- else:
45
- # Converte string de bytes para imagem
46
- frame = Image.open(io.BytesIO(frame_data))
47
- updated_frames.append(frame)
48
-
49
- # Atualiza o frame atual
50
- updated_frame = edit_frame(updated_frames[-1], color, x, y, pixel_size)
51
- updated_frames[-1] = updated_frame
52
-
53
- # Cria o GIF
54
- gif = create_animation(updated_frames, duration)
55
- return gif
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
- frames_input = gr.File(label="Frames Existentes (opcional)", file_types=["image"], file_count="multiple")
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
- output = gr.Image(label="Pré-visualização do Frame Atual", tool="editor")
71
- gif_output = gr.File(label="Animação Final (GIF)")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- generate_btn = gr.Button("Gerar Animação")
74
- generate_btn.click(
75
  editor,
76
- inputs=[frames_input, color_picker, x_input, y_input, pixel_size, duration],
77
- outputs=[gif_output]
78
  )
79
 
80
- # Executa o aplicativo
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()