IAimage / app.py
onnew's picture
Update app.py
bd1c40e verified
raw
history blame
2.58 kB
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()