[Python] How to create a GIF using pillow and imageio with python?

Discussão em 'Python' iniciado por Stack, Abril 5, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following code that creates 60 frames using pillow in python 3.11.10 and that I want to use to create a gif (repeating endless), with a duration per frame of 0.1 seconds. The first 4 frames should show a red square, and the rest of the time (almost 6 seconds) it should be basically black. However, the created GIF only seems to contain 2 frames, one frame with the red square and one without. How to create the gif properly?

    from PIL import Image, ImageDraw
    import imageio
    import numpy as np

    # Colors
    red = (255, 0, 0)
    black = (0, 0, 0)
    grey = (30, 30, 30)

    frames = []
    framerate = 10
    frame_count = 60
    for i in range(frame_count):
    # Create an image with black background
    img = Image.new('RGB', (55, 50), black)
    draw = ImageDraw.Draw(img)
    # Draw rectangle
    draw.rectangle((10, 20, 45, 30), fill=red if i<5 else grey)
    # Append frame to the list
    frames.append(np.array(img))
    # Save frames as a GIF
    imageio.mimsave("test.gif", frames, duration=1./framerate, loop=0, plugin='pillow')


    Here is the created gif:

    [​IMG]

    Expected behavior: 0.4 seconds showing that red square, then 5.6 seconds showing basically black.

    With Grismar's suggestion:

    [​IMG]

    Continue reading...

Compartilhe esta Página