Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert XCF to PNG using GIMP from the command-line?

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.

like image 575
Laurence Gonsalves Avatar asked Apr 26 '11 18:04

Laurence Gonsalves


People also ask

How do I convert XCF files?

How to Convert an XCF File. GIMP saves files to this format by default, but you can use the File > Export menu to save it as something else, like a JPG or PNG. You can also use a free image file converter like Zamzar to convert the file to PDF, GIF, AI, TGA, WEBP, TIFF, and other similar file formats.


1 Answers

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:

gimp -n -i -b - <<EOF (let* ( (file's (cadr (file-glob "*.xcf" 1))) (filename "") (image 0) (layer 0) )   (while (pair? file's)      (set! image (car (gimp-file-load RUN-NONINTERACTIVE (car file's) (car file's))))     (set! layer (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)))     (set! filename (string-append (substring (car file's) 0 (- (string-length (car file's)) 4)) ".png"))     (gimp-file-save RUN-NONINTERACTIVE image layer filename filename)     (gimp-image-delete image)     (set! file's (cdr file's))     )   (gimp-quit 0)   ) EOF 

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).

like image 141
Laurence Gonsalves Avatar answered Sep 28 '22 02:09

Laurence Gonsalves