> would you can send me the script? I am very interested about using GIMP to
> make/process animations, so a script that gets all the imagens in a
> directory, and apply some kind of transformation in them would be very
> useful for me. And I am still a Script-fu beginner :)
> thank you,
> andrei

Now *that* is a noble cause. ;-)

I'm not really all that crazy about Scheme (Gimp's built-in scripting 
language), so I don't write with it. I really, really like Python, though, 
and there are Python bindings for the Gimp. You can download them at  

Use the documentation there, plus the PDB browser, to figure out everything 
you can do. In the meantime, here's a quick little script I whipped up to 
rotate a series of images in a directory. To avoid writing something that 
would be trivial to do with "convert," I've put in a little twist... The 
first image is slightly rotated, the second a little bit more, and so on, 
until the last image, which goes to the user-specified rotation.



rotate.py -----------------------------------------------------------------

#!/usr/bin/env python
# rotate.py
# animate rotating all images in a directory; The first image gets rotated
# very little, progressing to the final image, which gets rotated by 
# a user-specified degree.
# Copyright 2002, Joel Hatch
# Licensed under the GNU GPL
# 18 June 2002

from gimpfu import *
import math, os, traceback

Error = "Error"

def rotateImages(directory, degrees):
   "Rotate images in a directory"

   # Make sure the directory is valid
   path = os.path.normpath(directory)
   if not os.path.isdir(path):
      raise Error, "Directory %s not found" % path

   # Get and sort a list of files in the directory
   oldDir = os.getcwd()
   files = os.listdir(path)
   # figure out how far to rotate each image; we are going to get a
   # group of images, and rotate each image further until the last image
   # is the full rotation. (convert to radians)
   standardRotation = (float(degrees) * math.pi) / (len(files) * 180)
   currentRotation  = 0
   for file in files:
         # load the image
         graphicFile = pdb.gimp_file_load(file, file) 

         # add an alpha channnel to the bottom layer, so the background 
         # will be transparent

         # calculate how big the containing box will need to be
         # (our picture is a rectangle, inside a circle (the rotation), 
         # inside a square (the containing box size). The width and
         # height of the containing box are the same as the diameter of
         # the circle, which can be found from the rectangle with d^2=w^2+h^2
         size = math.sqrt(graphicFile.width**2 + graphicFile.height**2)

         # calculate the top left position of our image inside the
         # containing box
         top  = (size - graphicFile.height)/2
         left = (size - graphicFile.width)/2

         # resize the image to the containing box
         pdb.gimp_image_resize(graphicFile, size, size, left, top)

         # rotate every layer in the image
         currentRotation = currentRotation - standardRotation
         for layer in graphicFile.layers:
            pdb.gimp_rotate(layer, FALSE, currentRotation)
            left, top = layer.offsets
            pdb.gimp_layer_resize(layer, size, size, left, top)

         # save and close the file
         if len(graphicFile.layers) > 1:
            finalLayer = pdb.gimp_image_merge_visible_layers(graphicFile, 
            finalLayer = graphicFile.layers[0]
         finalName = "n_%s.png" % os.path.splitext(file)[0]
         pdb.file_png_save(graphicFile, finalLayer, finalName, finalName, \
            FALSE, 6, TRUE, TRUE, TRUE, TRUE, TRUE)
# If you want to display the image, instead of closing it, comment the
# above line, and un-comment the line below
#         pdb.gimp_display_new(graphicFile)

   # change back to the default directory

   "Rotate all images in a directory",
   "Joel Hatch",
   "16 June 2002",
   "RGB*, GRAY*",
      (PF_STRING, "Directory",  "Directory containing files to rotate", ""),
      (PF_INT,    "degrees",    "Degrees to rotate the final image", "")



Gimp-user mailing list

Reply via email to