Monday, July 24, 2017

Fix Gimp with python script.

Today I will show you how python language can help GIMP users.
From my point of view, Gimp does not properly import frames from GIF files.
This program imports GIF files in this way:

Using the python module, you can get the correct frames from the GIF file.
Here's my script that uses the python PIL module.
import sys
from PIL import Image, ImageSequence
        img =[1])
except IOError:
        print "Cant load", infile

pal = img.getpalette()
prev = img.convert('RGBA')
prev_dispose = True
for i, frame in enumerate(ImageSequence.Iterator(img)):
    dispose = frame.dispose

    if frame.tile:
        x0, y0, x1, y1 = frame.tile[0][1]
        if not frame.palette.dirty:
        frame = frame.crop((x0, y0, x1, y1))
        bbox = (x0, y0, x1, y1)
        bbox = None

    if dispose is None:
        prev.paste(frame, bbox, frame.convert('RGBA'))'result_%03d.png' % i)
        prev_dispose = False
        if prev_dispose:
            prev ='RGBA', img.size, (0, 0, 0, 0))
        out = prev.copy()
        out.paste(frame, bbox, frame.convert('RGBA'))'result_%03d.png' % i)
Name the python script with and then you can use it on the GIF file as follows:
C:\Python27>python.exe 0001.gif
The final result has a smaller number of images than in Gimp, but this was to be expected.