XYK

GIFcapture

July 26th, 2012

Projects


GIFcapture animation.

GIFcapture is a little python script I wrote that captures anything on your screen to a .gif animation. It utilizes the Python Imaging Library for screen capturing and and the image2gif module by Almar Klein for the actual gif generation. This script is Windows only for now until I can find a msvcrt equivalent in Linux.

Instead of taking a video of the screen, the program captures many screenshots in succession to create frames. There is a inherent limitation to using this method: the capture rate is restricted by the write speed of the hard drive. To get around this, the program holds the screenshots in RAM until the user ends capturing.

There is still a lot of work to be done. A nice GUI interface will be added, and the code can be optimized even more. Check out the TODO file for a list of features that will be implemented.

information

• Requires PIL and python (tested with 2.7, not compatible with 3).
• User editable capture size and resize.
• Graininess is from the 8-bit color dithering. It is not recommended to turn it off, but you can by adding a dithering=0 parameter to the writeGif function.
• Maximum capture speed of ~20fps.
• Does NOT support dual monitors at the moment. It will only capture what happens on the primary screen.

download

Download GIFcapture version 0.91 here or on github. Updated 8-02.
GIFcapture_v0.91.zip

source

#! /usr/bin/python

import os
import shutil
import sys
import time
from PIL import Image
import ImageGrab
import etc.settings as s
from lib.images2gif import writeGif
import msvcrt

img = []
file_location = []
fps = []
time.clock() #reset process timer

program_path = os.path.dirname(sys.argv[0])+'/'
if(os.path.isdir('%stemp'%program_path) is not True): os.mkdir('%stemp'%program_path)

raw_input("\nPress enter to start capture.\n")

while(not msvcrt.kbhit()):
    old_time = time.clock()
    img.append(ImageGrab.grab(s.CAPTURE_DIMENSIONS))
    print 'Capturing frame %d'%len(img)
    time.sleep(s.CAPTURE_DELAY)
    fps.append(time.clock()-old_time)

print '\n\n  Captured %d frames in %.3fs'%(len(img), sum(fps))
print '  Frames captured per second: %.2f' %(1/(sum(fps)/len(fps)))
print '\nWriting frames to %s...'%s.GIF_FILE

for x in range(len(img)):
    file_location.append(r'%stemp\frame_%d.jpg'%(program_path,x))
    if(s.RESIZED_DIMENSIONS[0] is not -1): img[x].thumbnail(s.RESIZED_DIMENSIONS, Image.ANTIALIAS)
    img[x].save(file_location[x])

PILimages = [Image.open(x) for x in file_location]

writeGif(s.GIF_FILE, PILimages, duration = (sum(fps)/len(fps))) #real time framerate
#for filename in file_location: os.remove(filename)
#os.rmdir('temp')
shutil.rmtree('%stemp'%program_path)

print "\n\n\nGIF file information:"
print "  Size:             %d KB" %(os.path.getsize(s.GIF_FILE)/1024)
print "  Frames:           %d" %len(img)
print "  Gif frame rate:   %.1f fps" %(1/(sum(fps)/len(fps)))
print "  Gif duration:     %.3fs\n\n" %(len(img)*(sum(fps)/len(fps)))

time.sleep(8)