How do I make a sprite as a GIF image in Pygame?

1.6k views Asked by At

I have this idea for a game where you dodge projectiles while controlling a helicopter. I am wondering if you can make a sprite appear as a GIF image, or something along the lines of two images switching every fraction of a second. I know how to make a sprite appear as one image:

self.surf = pygame.image.load("example.png").convert()

But I was wondering if this had an effect:

self.surf = pygame.image.load("example.gif").convert()

Unfortunately, it only displayed the first image in the gif.

Here is the GIF image:

Helicopter.gif


Ok, so I looked at the answers and tried to implement them in my code, but then it was all too confusing and I had tried to do something a bit more simple. This is what I came up with:

    if play == 1:
        self.surf = pygame.image.load("Image2.png").convert()
        pygame.display.update()
        play = 2
        time.sleep(.2)
    if play == 2:
        self.surf = pygame.image.load("Image1.png").convert()
        pygame.display.update()
        play = 1
        time.sleep(.2)

But, all that did was display the player sprite as image 1. Is there anything I can add to make this work?

2

There are 2 answers

0
Rabbid76 On BEST ANSWER

Pygame respectively the pygame.image module can only handle non-animated GIFs.

But on the Pygame homepage is introduced the GIFImage library:

This library adds GIF animation playback to Pygame.


Another option is to use the Pillow library (pip install Pillow).

Write a function, that can convert a PIL image to a pygame.Surface: (see also PIL and pygame.image)

def pilImageToSurface(pilImage):
    mode, size, data = pilImage.mode, pilImage.size, pilImage.tobytes()
    return pygame.image.fromstring(data, size, mode).convert_alpha()

Use the PIL library to load a GIF frame by frame: (see also Extracting The Frames Of An Animated GIF Using Pillow

def loadGIF(filename):
    pilImage = Image.open(filename)
    frames = []
    if pilImage.format == 'GIF' and pilImage.is_animated:
        for frame in ImageSequence.Iterator(pilImage):
            pygameImage = pilImageToSurface(frame.convert('RGBA'))
            frames.append(pygameImage)
    else:
        frames.append(pilImageToSurface(pilImage))
    return frames

See also Load animated GIF and a simple animated gif viewer example:

import pygame
from PIL import Image

def pilImageToSurface(pilImage):
    mode, size, data = pilImage.mode, pilImage.size, pilImage.tobytes()
    return pygame.image.fromstring(data, size, mode).convert_alpha()

def loadGIF(filename):
    pilImage = Image.open(filename)
    frames = []
    if pilImage.format == 'GIF' and pilImage.is_animated:
        for frame in ImageSequence.Iterator(pilImage):
            pygameImage = pilImageToSurface(frame.convert('RGBA'))
            frames.append(pygameImage)
    else:
        frames.append(pilImageToSurface(pilImage))
    return frames

pygame.init()
window = pygame.display.set_mode((500, 500))
clock = pygame.time.Clock()

gifFrameList = loadGIF("my_gif.gif")
currentFrame = 0

run = True
while run:
    clock.tick(20)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    window.fill(0)

    rect = gifFrameList[currentFrame].get_rect(center = (250, 250))
    window.blit(gifFrameList[currentFrame], rect)
    currentFrame = (currentFrame + 1) % len(gifFrameList)

    pygame.display.flip()

You can use the list of pygame.Surface objects to generate a Spritesheet.

2
Henry Bass On

Pygame can't do GIF images, but if you really want to, you could animate it frame by frame one image at a time.