Python-Fu: Save different versions of an XCF file (branded, watermarked, thumbnail)

It took forever, but I finally hunkered down and learned some Python-Fu. It’s really nice, especially being able to use Python’s libraries, but, like programming Gimp in Scheme, it was kind of hard.

The most important thing, which I learned only after working at it three hours, is to use the “Browser…” tool in the Python console (in Gimp). This helps you find predefined commands to do all the expected operations. Use the “Apply” button to paste an example into the console. This is really, really important, because the documentation doesn’t show you the correct usage (or any usage).

The docs are really for Scheme, but the functions are used a little differently in Python.

What this tool does is take a directory full of XCF files, and produces a directory with thumbnails, decorated full-size images, and unadorned full-size images. The adornments are a logo and caption, which are in layers named “logo” and “caption”.

The script hides those layers automatically, to produce both versions.

The only “trick” I had to figure out is that you can’t simply export a JPEG. The Gimp save functions save a drawable (a layer) rather than the entire image. So you need to flatten the image and save that layer. The problem is, flatteing is destructive: if you flatten the image, you can’t toggle the adornment layers.

The solution is to make a new image use the pdb.gimp_layer_new_from_visible() call to produce a new layer that’s associated with this new image. Then, save that new image and the new layer.

  newimg = gimp.Image(img.width, img.height, RGB)
  layer = pdb.gimp_layer_new_from_visible(img, newimg, "Composite")
  pdb.file_jpeg_save(newimg, layer, filename, filename, .95, 0, 1, 1, '', 0, 0, 0, 0)

The code here is terrible, but it’s also terribly useful.

Attachment Size
riceball_resize.py_.txt 4.49 KB