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, sys, gif_pygame

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

s1 = pygame.Surface((66, 66))
s2 = pygame.Surface((66, 66))
s3 = pygame.Surface((66, 66))
s1.fill((255, 0, 0))
s2.fill((0, 255, 0))
s3.fill((0, 0, 255))

example_surfs = gif_pygame.GIFPygame([(s1, 1), (s2, 1), (s3, 1)])

clock = pygame.Clock()

while True:
    clock.tick(60)
    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))
    win.blit(example_surfs.blit_ready(), (384-example_surfs.get_width()*0.5, 256-example_surfs.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: # Check whether `example_gif` is paused or not
                    example_gif.unpause() # unpauses `example_gif` if it was paused
                else:
                    example_gif.pause() # pauses `example_gif` if it was unpaused

                if example_png.paused: # Check whether `example_png` is paused or not, since this is a non-animated image, it will not be affected
                    example_png.unpause() # unpauses `example_png` if it was paused, since this is a non-animated image, it will not be affected
                else:
                    example_png.pause() # pauses `example_png` if it was unpaused, since this is a non-animated image, it will not be affected

                if example_surfs.paused: # Check whether `example_surfs` is paused or not
                    example_surfs.unpause() # unpauses `example_surfs` if it was paused
                else:
                    example_surfs.pause() # pauses `example_surfs` if it was unpaused
                    
    pygame.display.flip()

To recap:

gif_pygame.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:

  • GIFPygame.get_width(), returns the width of the image
  • GIFPygame.get_height(), returns the height of the image
  • GIFPygame.get_size(), returns the size of the image
  • GIFPygame.get_rect(), returns the rect of the image
  • GIFPygame.get_surfaces(), returns a list of all surfaces in the animation, you can also pass in certain indexes
  • GIFPygame.set_surface(), replaces some of the surfaces in the animation with newer surfaces
  • .get_durations(), returns a list of all durations in the animation, you can also pass in certain indexes
  • gif_pygame.transform(), a sublibrary for editing the surfaces.

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

(A documentation will be added soon)

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-1.1.1.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

gif_pygame-1.1.1-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for gif_pygame-1.1.1.tar.gz
Algorithm Hash digest
SHA256 2a9c4fc040ab80cd481bc5c50e6fbd4be5fea2f28c02bb787fa608e3f4b0c80f
MD5 64e707d221012c5665516e575c72eb95
BLAKE2b-256 5cdfb46fe06048aa957e4db9f3c28bc81f8f0d9954e53a62796da382e5501359

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for gif_pygame-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 200902923e3d7375927a6028fd45642642c522c7d7d03661456d87bfda531d72
MD5 0839053fcd3e7f8be5a55abaa4255924
BLAKE2b-256 d5fab27798c8c822d338350d9996d0e0c2203242d1c97089da32ba359bd966fd

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