Code Realm

Log In

Main Page

Code Page

Supybot Plugins

Convert Gimp XCF Page

AusImage Realm Code Projects

I found a need to be able to convert Gimp files to another format in batch mode. I tried with little success the script-fu script engine native with Gimp. I then discovered that ImageMagick had the capability to do the conversion. With prior bash experience and help from some mates on `IRC, I have managed to cobble together a Bash shell script.

My coding task was still difficult. The ImageMagick convert command did not always produce the results expected. I discovered that image and text layers in Gimp `XCF format through the conversion.

I tried many combinitions of command options available for the convert command. Each combination worked on certain files. I came up with a 'works most of the time' set of options. It still throws warnings but produces the expected results.

Using Convert Basics

# Convert xcf file to an intermediary that supports layers
convert -geometry 800x800 -deconstruct -coalesce \
    xcf:photo.xcf.gz tiff:photo.tiff;

# Convert from intermediary layer 0 to final jpg image
convert tiff:photo.tiff[0] jpg:photo.jpg;

This was my 'works most of the time' solution. I threw in an intermediary image that supported layers to produce the final result from. Layers in Gimp `XCF format can show only the top most layer, which with a mostly transparent layer is mostly black. ImageMagick seems to expect all layers to be the same size, but this method at least returns the correct layer.

Using Convert and Composite Advanced

# Convert xcf file to an intermediary that supports layers
# Overlay text label just above center bottom
convert -geometry 800x800 -deconstruct -coalesce \
    -font /fonts_dir/myfont.ttf -fill orange -pointsize 21 \
    -gravity south -annotate +0+15 'photo label' \ 
    xcf:photo.xcf.gz tiff:photo.tiff;
# Composite logo overlay just inside bottom right corner
# Output intermediary layer 0 to final jpg image
composite png:/img_files/logo.png tiff:photo.tiff[0] \
    -gravity southeast -geometry +15+15 -compose over \
    jpg:$file.jpg;

My next task was to include a text label and a logo on each image. ImageMagick seems to make this fairly straight forward with help from their many examples. I added the additional options to the convert command to overlay the text a distance from the bottom center. I subsituted the last convert command for the composite command to make a logo image overlay a distance from the bottom right corner.

Using the commands over many files

The following are two scripts that enabled me to batch process a directory or directories of Gimp `XCF format files. The only error I got was 'convert: images are not the same size', but I did get the majority of my files converted and labled with these scripts.

Script to cycle over a single directory.


#!/bin/sh
# Use glob pattern to find matching files
for img in *.xcf.gz

    file=${img%.xcf.gz};

    # Convert xcf file to an intermediary supporting layers
    # Overlay text label just above center bottom
    echo "$file is being converted...";
    convert -geometry 800x800 -deconstruct -coalesce \
        -font /fonts_dir/myfont.ttf  -fill orange \
        -pointsize 21 -gravity south -annotate +0+15 \
        'photo label' xcf:$file.xcf.gz tiff:$file.tiff;

    # Composite logo overlay just inside bottom right corner
    # Output intermediary layer 0 to final jpg image
    echo "$file is being overlayed...";
    composite png:/img_files/logo.png tiff:$file.tiff[0] \
        -gravity southeast -geometry +15+15 -compose over \
        jpg:$file.jpg;

    rm $file.tiff

done;

Script to cycle over multiple directories.


#!/bin/sh
# Use find command to locate matching files in dirs beneath
for img in `find . -name \*.xcf.gz`; do 

    file=${img%.xcf.gz};

    # Convert xcf file to an intermediary supporting layers
    # Overlay text label just above center bottom
    echo "$file is being converted...";
    convert -geometry 800x800 -deconstruct -coalesce \
        -font /fonts_dir/myfont.ttf  -fill orange \
        -pointsize 21 -gravity south -annotate +0+15 \
        'photo label' xcf:$file.xcf.gz tiff:$file.tiff;

    # Composite logo overlay just inside bottom right corner
    # Output intermediary layer 0 to final jpg image
    echo "$file is being overlayed...";
    composite png:/img_files/logo.png tiff:$file.tiff[0] \
        -gravity southeast -geometry +15+15 -compose over \
        jpg:$file.jpg;

    rm $file.tiff

done;

Contact with Questions or Feedback: Send email to tjaustinbardo AT gmail DOT com

Creative Commons Attribution-NonCommercial-ShareAlike License

modified: December 05, 2007, at 12:12 AM