I'm trying to create a gif from pygame's frames saved using pygame.image.save
, lets say that frames are already saved in runtime directory and then I use this code:
file_names = sorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
images = [Image.open(fn) for fn in file_names]
filename = "my_gif.GIF"
writeGif(filename, images, duration=1.0, dither=0)
there is no error after running that and .gif file weights 21 KB but everything I can see is black screen (even though .png files don't have anything in black color).
However previously there was an error in images2gif.py:
fp.write(globalPalette)
TypeError: must be string or buffer, not None
and I fixed that using method from the best answer in this question: Error in images2gif.py with GlobalPalette
and now it doesn't raise any errors but .gif file is entirely black.
I use Python 2.7.6
Here is a full code to generate .png and then .gif:
import pygame, sys, os
from images2gif import writeGif as writeGif
from PIL import Image
from pygame.locals import *
def main():
pygame.init()
surface = pygame.display.set_mode((800, 600), 0, 32)
surface.fill((255,0,0))
center = (800 // 2, 600 // 2)
count = 1
while (count < 26):
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.draw.rect(surface, (0, 255, 255), (center[0], center[1] + (count * 5), 5, 5))
filename = 'frame-%03d.png' % (count)
count = count + 1
pygame.image.save(surface, filename)
pygame.quit()
file_names = sorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
images = [Image.open(fn) for fn in file_names]
filename = "my_gif.GIF"
writeGif(filename, images, duration=1.0, dither=0)
sys.exit()
main()
Maybe there is a better way to create a gif or movie from these .png files? I don't have to use images2gif.py