File size: 2,578 Bytes
bd1c40e
 
 
 
 
 
 
 
 
1616045
bd1c40e
 
 
 
 
 
 
 
 
 
1616045
bd1c40e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1616045
bd1c40e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import gradio as gr
from PIL import Image, ImageDraw, ImageSequence
import io

# Função para criar ou editar frames
def edit_frame(frame, color, x, y, pixel_size):
    if frame is None:
        # Cria um frame vazio
        frame = Image.new("RGBA", (64, 64), (255, 255, 255, 0))
    else:
        frame = frame.convert("RGBA")
    
    # Desenha o pixel
    draw = ImageDraw.Draw(frame)
    draw.rectangle(
        [x * pixel_size, y * pixel_size, (x + 1) * pixel_size - 1, (y + 1) * pixel_size - 1],
        fill=color,
        outline=color
    )
    return frame

# Função para criar uma animação GIF
def create_animation(frames, duration):
    if not frames:
        return None
    output_gif = io.BytesIO()
    frames[0].save(
        output_gif,
        format="GIF",
        save_all=True,
        append_images=frames[1:],
        duration=duration,
        loop=0
    )
    output_gif.seek(0)
    return output_gif

# Função principal para o Gradio
def editor(frames, color, x, y, pixel_size, duration):
    updated_frames = []
    for frame_data in frames:
        if frame_data is None:
            updated_frames.append(None)
        else:
            # Converte string de bytes para imagem
            frame = Image.open(io.BytesIO(frame_data))
            updated_frames.append(frame)

    # Atualiza o frame atual
    updated_frame = edit_frame(updated_frames[-1], color, x, y, pixel_size)
    updated_frames[-1] = updated_frame

    # Cria o GIF
    gif = create_animation(updated_frames, duration)
    return gif

# Interface Gradio
with gr.Blocks() as demo:
    gr.Markdown("# Editor de Sprites e Animações")

    with gr.Row():
        frames_input = gr.File(label="Frames Existentes (opcional)", file_types=["image"], file_count="multiple")
        color_picker = gr.ColorPicker(label="Cor")
        x_input = gr.Number(label="X", value=0, precision=0)
        y_input = gr.Number(label="Y", value=0, precision=0)
        pixel_size = gr.Slider(label="Tamanho do Pixel", minimum=1, maximum=10, value=1)
        duration = gr.Slider(label="Duração do Frame (ms)", minimum=50, maximum=500, value=100)

    with gr.Row():
        output = gr.Image(label="Pré-visualização do Frame Atual", tool="editor")
        gif_output = gr.File(label="Animação Final (GIF)")

    generate_btn = gr.Button("Gerar Animação")
    generate_btn.click(
        editor,
        inputs=[frames_input, color_picker, x_input, y_input, pixel_size, duration],
        outputs=[gif_output]
    )

# Executa o aplicativo
if __name__ == "__main__":
    demo.launch()