Advertisement
drandreasdr

Generating a GIF animation using Python

Nov 4th, 2014
3,535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. # A short example in which a GIF animation is created from PNG images using subprocess and ImageMagick's convert.exe.
  2. # pygame is here used to generate the PNG images.
  3. # Andreas Draganis, 2014-11-04
  4.  
  5. import pygame
  6. import subprocess
  7. import os
  8.  
  9. #Create a bunch of images using pygame and save them on the disk as PNG files:
  10. pygame.init()
  11. IMGSIZE = 100
  12. displaysurf = pygame.display.set_mode((IMGSIZE, IMGSIZE))
  13. imagelist = []
  14. filenamelist = [0]*5
  15. for i in range(5):
  16.     pygame.draw.rect(displaysurf, (0, 0, 0), pygame.Rect(0, 0, IMGSIZE, IMGSIZE), 0)
  17.     pygame.draw.rect(displaysurf, (255, 255, 255), pygame.Rect(i*20, i*20, 20, 20), 0)
  18.     #Save as PNG images on disk:
  19.     filenamelist[i] = "pic" + str(i) + ".png"
  20.     pygame.image.save(displaysurf, filenamelist[i])
  21.  
  22. #Combine into a GIF using ImageMagick's "convert"-command (called using subprocess.call()):
  23. convertexepath = "C:/Program Files (x86)/ImageMagick-6.8.9-Q16/convert.exe"  # Hardcoded
  24. convertcommand = [convertexepath, "-delay", "10", "-size", str(IMGSIZE) + "x" + str(IMGSIZE)] + filenamelist + ["anim.gif"]
  25. subprocess.call(convertcommand)
  26.  
  27. #Remove the PNG files (if they were meant to be temporary):
  28. for filename in filenamelist:
  29.     os.remove(filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement