Skip to main content

A pygame addon for animated image files

Project description

gif_pygame

A pygame addon that allows you to load, animate, and render animated image files like .gif and .apng

How to use

# example code

import pygame, pygame_gif, sys

win = pygame.display.set_mode((512, 512))
example_gif = pygame_gif.load("example.gif") # Loads a .gif file
example_png = pygame_gif.load("example.png") # Loads a .png file, the module supports non-animated files, but it is not recommended
example_apng = pygame_gif.load("example.apng") # Loads a .apng file

while 1:
    win.fill((0, 0, 0))
    
    # There are 2 ways of rendering the animated img file, the first method is doing "gif.render(surface, (x, y))", the other method is doing "surface.blit(gif.blit_ready(), (x, y))". THE "blit_ready()" FUNCTION MUST BE CALLED WHEN DOING THE SECOND METHOD
    example_gif.render(win, (128-example_gif.get_width()*0.5, 256-example_gif.get_height()*0.5))
    example_png.render(win, (256-example_png.get_width()*0.5, 256-example_png.get_height()*0.5))
    example_apng.blit(example_apng.blit_ready(), (384-example_apng.get_width()*0.5, 256-example_apng.get_height()*0.5))


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit()

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                if example_gif.paused:
                    example_gif.unpause()
                else:
                    example_gif.pause()
                if example_png.paused:
                    example_png.unpause()
                else:
                    example_png.pause()
                if example_apng.paused:
                    example_apng.unpause()
                else:
                    example_apng.pause()
                    
    pygame.display.update()

To recap:

pygame_gif.load loads in the image

To render the image you've got 2 options:

  • img.render(surf, (x, y))
  • surf.blit(img.blit_ready(), (x, y)) (.blit_ready() can be used to return the current frame's surface)

There are other extra functions. The ones showcased in the example code are img.pause() and img.unpause().

There are also:

  • .get_width(), returns the width of the image
  • .get_height(), returns the height of the image
  • .get_size(), returns the size of the image
  • .get_rect(), returns the rect of the image
  • .get_surface(), returns a list of all surfaces in the animation, you can also pass in certain indexes
  • .set_surface(), replaces some of the surfaces in the animation with newer surfaces
  • .get_duration(), returns a list of all durations in the animation, you can also pass in certain indexes
  • .set_duration(), replaces some of the durations in the animation with newer durations
  • .get_data(), returns a list of all surfaces and durations in the animation, you can also pass in certain indexes
  • .set_data(), replaces some of the surfaces and durations in the animation with newer surfaces and durations
  • .get_alpha(), returns a list of that includes the alphas of all surfaces in the animation, you can also pass in certain indexes
  • .set_alpha(), replaces all the alphas of surfaces with newer alphas, you can also pass in certain indexes
  • .convert(), converts all the surfaces in the animation, you can also pass in a colorkey and certain indexes

Please use python's help() function for more in-depth explanation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gif_pygame-0.0.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

gif_pygame-0.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file gif_pygame-0.0.1.tar.gz.

File metadata

  • Download URL: gif_pygame-0.0.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for gif_pygame-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a5e43fc7f2ed9a512fd343775564ae25d5a1fe0963dd09090089ac948127e76b
MD5 0cc9592b79083bcd7a855e332cc17a1b
BLAKE2b-256 fd39e41adb4d93e84e8714d9ce49f8032e5312a7b496ba04928a22e194b179d9

See more details on using hashes here.

File details

Details for the file gif_pygame-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: gif_pygame-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for gif_pygame-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7fe56c22b84c06e9322348b4dba5013f58250e01093710fd4772f68af1dc5235
MD5 a8a251c210971a3549b512c9e3f6edf0
BLAKE2b-256 43fbebf79b38287beb5a3e043e031dfb4549c118d3d6743d2241cca4fd30586a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page