How to make an animated gif from a Gimp picture

How to make an animated gif from a Gimp picture 

I have a slightly different topic today, not about drawing, a little more technical. In my first blog post, the one about illustrating the book Into Exaltia (you can find the post here, if you are interested), there is an animated gif picture, showing how I made one of the illustrations. By “showing” I mean “showing in a very simplified way”. I’ve used three stages of the illustration – pencil version, half inked and fully inked and gradually transformed each into the next one.

And now for the more technical stuff

As you may or may not now, animation is just a series of pictures that are shown very quickly and you see them as movement. As far as I know, the most usual speed is 24 pictures per second (or frames per second, FPS). I had an image in Gimp format, saved as .xcf, consisting of three layers. I wanted to start with only the bottom layer visible (the two top layers had opacity 0 in Gimp). Then, by adding the opacity to the middle layer, it would become more and more visible. When it was fully visible, I would start adding the opacity to the top layer, until it reaches 100. This should result in seeing the picture transform from the first stage to the last one. The only problem is, how to get the frames for the animation. You can do it by hand, change the opacity manually by 10 and save the image. But that doesn’t sound reasonable, when computers are made to repeat boring tasks like that.

Python fu

I was googling a little and I found out that you can do some programming to make Gimp do what you want (like change opacity of layers and save images/frames). So, I have written a little code in Python fu (it took me a few days, mostly because I’ve had no experience with programming in Python). From now, all I need to do is set the size of the step (if I set it to 10, Gimp repeatedly adds 10 to opacity and saves the image) and Gimp will vomit all the frames I need into a folder. You can say that I could have done the frames by hand in 10 minutes, instead of spending days on writing the Python code. And you are right. But where’s the fun in that?

Besides, now, every time I have a picture with layers, I can use the code and create the frames really easily. I think I will have to make lots of animated pictures now, to justify the time I spent on the Python code.

The danger of using computer to export images

It wasn’t obvious (to me) from the start, but there is a danger in generating animation frames on computer. It can easily get out of hand. My first attempt was great, I have exported 100 frames, played them at 24 frames per second speed and I had a nice little animation with very smooth transitions/movements. It looked great. And the gif file was too big. You can easily make thousands of frames like this. Which is great if you don’t care about the size of the final file. But I wanted to put the gif to my website, so I have exported only 50 frames and played them at 12 frames per second (compared to the previous version, the step was twice as big and I played each frame twice). It doesn’t look so good. But I reduced the size of the gif from 17 MB to 7 MB.

How does it actually work

Ok, now to the final part. Below, you can see the Python Fu code that I have used. I put some comments into it, so it should be clear what does which part of the code do. If you want to use the code, you need to do this:

  1. Make sure you have your xcf file with layers, you should turn the opacity of the bottom layer to 100 (this won’t be changed at all in the code). It doesn’t matter how many layers does it have, but there needs to be at least one.
  2. Copy the code below to a text editor so that you can make changes in it
  3. Paste the full path to the xcf file into the code (line 2)
  4. Set the step, how much opacity you want to add in each frame, it is now set to 2, you can choose number from 0 to 100 (line 5)
  5. Set the path to the folder, where the images/frames should be saved (line 7)
  6. Open Gimp (empty image, or any other image, doesn’t matter) and go to Filters  -> Python-Fu -> Console
  7. Copy the whole code (after the changes you made in 3, 4 and 5) to the console and press enter
  8. The folder you set at 5 should now be full of images

Turning the pictures into the gif or video can be done in many ways.

If you are a Mac user, you can use these instructions. In Windows, I think Windows Movie Maker can turn images into video. Or you can use Gimp, open all the frames as layers in Gimp and then save as animated gif.



# copy a path to your xcf file
# n is the step/change in opacity between frames as the opacity goes from 0 % to 100 % at each layer
# Example: if n=50 and you have an image with three layers, it means that you get five frames with opacities 100/0/0, 100/50/0, 100/100/0, 100/100/50, 100/100/100
# set path to the output - a folder where all the exported frames will be saved

# converts a number to a four digit string format
# Example: convertnum(15)=0015
def convertnum(a):
    if a<10:
        return "0000" + str(a)
    elif a<100:
        return "000" + str(a)
    elif a<1000:
        return "00" + str(a)
    elif a<10000:
        return "0" + str(a)
        return str(a)

# load the image
# display the image
# get a list of layers of the image

# set opacity of all layers except the bottom one to 0 (this will be the first frame)
for i in range(len(layers)-2,-1,-1):

# create frames by gradually adding opacity to next layers
# each layer will get from 0 opacity to 100 in n steps
for i in range(len(layers)-2,-1,-1):
    for j in range(0,100+n,n):
        cesta=temp + "/frame" + convertnum(poc) + ".jpg"
        Lpom = pdb.gimp_layer_new_from_visible(image, image, "pomlayer")
        pdb.file_jpeg_save(image, Lpom, cesta, cesta, 0.8, 0, 0, 0, "", 0, 0, 0, 0)

Leave a Reply

Your email address will not be published. Required fields are marked *