As part of my build process I need to convert a number of XCF (GIMP's native format) images into PNG format. I'm sure this should be possible using GIMP's batch mode, but I have forgotten all of the script-fu I used to know.
My input images have multiple layers, so I need the batch mode equivalent of "merge visible layers" followed by "save as PNG". Also note that I can't install anything in ~/.gimp*/scripts/
— I need a self-contained command-line, or a way to install scripts in my source tree.
Note that while this is similar to this question, I have the additional constraint that I need this to be done using GIMP. I tried the current version of ImageMagick and it mangles my test images.
I know this is not strictly the correct answer but seeing as a I got directed here while searching.
I modified the example code here to create a workable plugin
http://registry.gimp.org/node/28124
There is a bit of code from another answer on this page
Before jsbueno posted his answer I had also tried asking on the #gimp IRC channel. I was directed to this thread on Gimptalk which contains the following code:
This scriptfu globs for xcf files, and then for each file it loads the file, merges the visible layers, saves the result as a PNG, and "unloads" the image. Finally, it quits GIMP. The glob approach is used to avoid starting up GIMP for each image. It also side-steps the issue of getting parameters from the shell into gimp.
I'm posting this answer just in case someone needs a way to do this without the use of GIMP-Python (perhaps because it isn't installed).
Use xcftools
It's possible that this tool wasn't available (or in any case widely known) at the time this question was originally asked, but the
xcf2png
terminal command from thexcftools
package does exactly what you ask and is very good. I use it for my thesis scripts and would be my go-to choice for this question.From the documentation:
(PS. I spotted
xcftools
mentioned in the comments just after I decided to post an answer about it, but I'm posting anyway as that comment was easy to miss (I saw it because I specifically checked for it) and I think xcftools deserves a proper answer's slot, as it really is the most appropriate answer at this point in time.)There are a few ways to go through this - my preferred method is always developing a GIMP-Python plug-in. Script-fu uses GIMP's built-in scheme implementation, which is extremely poor in handling I/O (such as listing files in a directory) - a task which is absolutely trivial in Python.
SO, since you said you don't want to save new scripts (you could do it because you can add new plug-in and scripts directories on edit->preferences->folder options so that you don't need to write on ~/.gimp*/plugins/)
Anyway, you can open the python console under filters, and paste this snippet in the interactive prompt.
This small function will open any image, passed as a file path, flatten it, and save it as a png with the default settings, in the same directory.
Following the function, you can simply type:
On the interactive prompt to convert all .xcf files on the current directory to .png
(If you don't have gimp-python installed, it may be a separate package on your linux distribution, just install it using your favorite tool - for those under windows, and gimp-2.6, the instructions on this page have have to be followed - it should become easier on gimp 2.8)
Another way, altogether, applies if your images are sequentially numbered in their filenames, such as myimage_001.xcf, mymage_002.xcf and so on. If they are so arranged you could install GIMP-GAP (gimp animation package) which allows one to apply any filters or actions in such an image sequence.
Here is my solution utilizing GIMP, GIMP's python-fu and bash. Note that GIMP's python-fu can only be run under a
gimp
process, not under plain python.