Converting XCF and other files using command line with GIMP?
Asked Answered
L

6

23

If I have an XCF file (or any other supported by Gimp) how can I convert it to, for example, PNG for display or further processing?

Lanugo answered 10/3, 2009 at 14:15 Comment(2)
If you want to know how to do this with GIMP rather than ImageMagick (which mangled my test images) take a look at this question.Corporeity
@Laurence Gonsalves, thanks! This is what I was originally looking for!Abraham
W
30

I'm a couple of years late, but I thought I'd add what I think is by far the best solution: there is a tool suite called Xcftools (on Ubuntu, apt-get install xcftools), which has a utility called xcf2png that does this job perfectly.

xcf2png image.xcf -o image.png

This is much better than a) using ImageMagick (which as I said in a comment above is horribly broken), or b) using Gimp (which has an extremely complicated scripting language for simply exporting an image).

Whit answered 12/6, 2011 at 7:59 Comment(2)
This worked well for me until recently. Using xcftools 1.0.7 and Gimp 2.10.22, I get Warning: XCF version 11 not supported (trying anyway...) and the output is a fully transparent image.Shoifet
yep, horribly outdated unfortunately, so ImageMagick it is thenRenzo
R
7

I guess ImageMagick should do what you want (and even more)

convert image.xcf image.png
Ragucci answered 10/3, 2009 at 14:20 Comment(4)
From my experience, current versions of ImageMagick have very buggy support for XCF. Various layer sizes cause the resulting image to be completely black. I also experience alpha channel bugs (solid pixels appearing from nowhere).Helminthology
Yeah, ImageMagick's XCF support is seriously broken. Using gimp itself would be a better option.Corporeity
This didn't work for me at all. It seems to be wanting to export layers as separate images (it created a name-0.png, name-1.png, for each layer), but it only extracted the first two layers and changed alpha to binary. So I'd rather use GIMP. Just can't for the life of me figure out what Lisp code to write...Whit
Using -flatten creates a single image, which solves the problem of ImageMagick saving each layer as an individual image.Barreto
P
6

A very good solution (and explanations!) can be found here. In short, there is a bash script feeding scheme/lisp to gimp

#!/bin/bash
{
cat <<EOF
(define (convert-xcf-to-jpeg filename outfile)
  (let* (
     (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
     (drawable (car (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)))
     )
    (file-jpeg-save RUN-NONINTERACTIVE image drawable outfile outfile .9 0 0 0 " " 0 1 0 1)
    (gimp-image-delete image) ; ... or the memory will explode
    )
)

(gimp-message-set-handler 1) ; Messages to standard output
EOF

for i in *.xcf; do
  echo "(gimp-message \"$i\")"
  echo "(convert-xcf-to-jpeg \"$i\" \"${i%%.xcf}.jpg\")"
done

echo "(gimp-quit 0)"
} | gimp -i -b -

But look at the page for the full story. It's worth it.

Plimsoll answered 6/2, 2015 at 11:27 Comment(0)
P
2

Very few, if any, programs other than GIMP read XCF files. This is by design from the GIMP developers, the format is not really documented or supported as a general-purpose file format.

That being said, look into using GIMP itself, using command line arguments (especially the --batch option).

EDIT: It looks as if ImageMagick does support XCF, so that is probably an easier route if the support seem so be good enough. I haven't tested it, and the documentation doesn't say much. I would be a bit wary.

Publicness answered 10/3, 2009 at 14:21 Comment(1)
Looks like imagemagick's convert works. I would prefer using GIMP since it surely supports its own format better, and also supports other formats. I have tried looking into GIMP from shell but it's a bit unintuitive. Can you perhaps drop an example or two on converting with GIMP,or a link with docs?Abraham
S
0

Bash oneliner. Only needs convert (which usually already installed from imagemagick).

convert -alpha on -background none -layers merge t2.xcf t2.png

Script that iterates over xcf files to convert them with appropriate names:

for i in *.xcf; do convert -alpha on -background none -layers merge $i ${i/xcf/png}; done
Singsong answered 8/2, 2022 at 16:36 Comment(0)
A
-1

If you want to bulk convert .xcf images to .png, you might find this wrapper script more useful:

#! /bin/bash -peux
exec xcf2png $1 -o ${1%.xcf}.png

Save it somewhere on your PATH as xcftopng (note the to instead of 2) and call it like so:

ls *.xcf|xargs -l xcftopng
Alexia answered 15/8, 2014 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.