Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Automated batch xcf layer combining and png exporting
#1
Big favor to ask of you scripting/programming gurus. I have three directories full of Gimp XCF layers and need help automating the combining of them, and exporting the resulting images as PNGs.

I'm agnostic to what language(s) are used, as long it'll run on a mac, specs below.

So the idea is to sequentially loop through the three directories, load one respective XCF layer from each, and export the three-layer combination as a PNG in a 4th directory.  The PNG filename could be a concatenation of the three XCF filenames used for each resultant image.

Directory structure looks like this:

ProjectName
  Layer1Files (top layer)
    A0.xcf
    A1.xcf
    A2.xcf
    A3.xcf
    A4.xcf
  Layer2Files (middle layer)
    B0.xcf
    B1.xcf
    B2.xcf
    B3.xcf
    B4.xcf
  Layer3Files (background layer)
    C0.xcf
    C1.xcf
    C2.xcf
    C3.xcf
    C4.xcf
  CombinedPngs (to be created)
    A0B0C0.png
    A0B0C1.png
    ...
    A0B0C4.png
    A0B1C0.png
    A0B1C1.png
    etc.

And so forth until we get from A0B0C0.png all the way to A4B4C4.png.

Mac is running Catalina 10.15.7
Gimp 2.10.24 per the Mac binary download.
Python 2.7.16 per MacOS default. (Though I understand this version is obsolete.)
Bash 3.2 is accessible (though zsh 5.7.1 is the default).
Scheme I've see mentioned but know nothing about.
Same with Fu.
Happy to upgrade and/or install whatever's needed to get this done.

THANK YOU to anyone willing to help with this!
Reply
#2
Maybe one of the clever guys can come up with a Gimp script, I think ImageMagick (IM) more probable. IM7 reads a Gimp 2.10 file (at least it does here in linux) You could ask the question on the IM forum where there is a MacOS user.

https://github.com/ImageMagick/ImageMagick/discussions

Some clarification on file properties will help
Are all the images the same size ?
or do you need to place top / middle layers in specific places ?
Images are to be stacked before writing to png, so these layers are partially transparent ?
Reply
#3
If instead of using XCF you use PNG, you can use ImageMagick's convert command in a Bash shell script (ie, the "Terminal" on OSX), then the whole thing becomes:

Code:
#! /bin/bash

botDir=$1
midDir=$2
topDir=$3
outDir=$4

function rootName {
   echo $(basename "${1%%.*}")
}

for bot in $botDir/*.png
do
   for mid in $midDir/*.png
   do
       for top in $topDir/*.png
       do
           out="$outDir/$(rootName "$bot")-$(rootName "$mid")-$(rootName "$top").png"
           printf "Generating %s\n" "$out"
           convert "$bot"  "$mid"  -composite "$top"  -composite "$out"
       done
   done
done
And you call it it as:
Code:
./generate /path/to/bottomDir /path/to/middleDir /path/to/topDir /path/to/outDir


   

PS:

  • Code above would also work for XCF (with appropriate .png ➤ .xcf changes) , but you have to make sure that your version of ImageMagick handles the XCF format (which likely means to make sure that the XCFs are in the 2.8 format)
  • With some recent versions of ImageMagick convert has become magick convert
Reply
#4
ImageMagick  7 reads .xcf here, so without any paths.to.. or loops or variables  and assuming a stack of same size / no offsets / transparent top & mid  and a background .xcf for a single combined.png

Code:
magick composite   top.xcf mid.xcf  temp.miff
magick composite  temp.miff back.xcf combined.png
rm temp.miff

Edit, just the bare bones to avoid a conversion to png which AFAIK is needed for IM 6 which does not support the Gimp 2.10
.xcf image format. Edit again, I see Ofnuts has a ps about that. Good script for the IM toolbox.
Reply
#5
Wow, can't thank you guys enough.  Here are some answers, results, and new questions. 

Rich:
> Are all the images the same size ?
The top layer is smaller than the middle which is smaller than the bottom.
> or do you need to place top / middle layers in specific places ?
They should just need to be stacked in order.  I pre-aligned each layer to a common sized boundary.  I wanted to simplify the automation effort to compositing only, and avoid any scripted image manipulation, at least for now.  Some alignment woes described below.
> Images are to be stacked before writing to png, so these layers are partially transparent ?
Yes.

Ofnuts:
Awesome shell script and sample output!  Can't tell you how exciting it is to have my output directory fill up with these combinations.

Both:
Yes ImageMagick 7.1.0-6 chokes on v2.10 XCFs. So I tried saving my XCF layers as pre-2.10. The only obvious method I found is to uncheck the last option when Saving, which leads to the Gimp complaint "Layer mode 'Normal' was added in GIMP 2.10".

When I saved the layers like this, ImageMagick could combine them, but sometimes layer alignment was lost in the resulting PNGs. I spent a bunch of time going in and re-aligning each image layer in Gimp and saving as pre-2.10 XCFs. But somehow the results were inconsistent.  So I ended up just going with PNG layers, which all aligned correctly when combined with the script.

Questions:
Is there a better way to enforce layer alignment than pre-aligning each layer to a common sized boundary?  (I never paid much attention to this, it always just worked right when doing things manually in Gimp.)
What's the preferred way to save v2.8 XCFs?
Gimp doc says PNG is lossless here, but when exporting to PNG, Gimp offers a compression slider, defaulting to 9. Is 9 lossless? Or maybe 0?
Reply
#6
Quote:What's the preferred way to save v2.8 XCFs?
Gimp doc says PNG is lossless here, but when exporting to PNG, Gimp offers a compression slider, defaulting to 9. Is 9 lossless? Or maybe 0?

The Gimp save dialogue has a message at the bottom of its window with information on compatibility.

Most often it is the layer mode which has a default / legacy switch but the Bit depth also comes into play in Image -> Precision Check that 8 bit & perceptual(sRGB) is set.

   

Shame the MacOS IM v7 does not rcognise Gimp 2.10 .xcf  however seems pointless to me to re-save all those .xcf to an earlier format. Export what you have straight to .png   PNG is lossless, the compression slider just applies the degree of compression. Lower compression = faster loading.

Quote: Is there a better way to enforce layer alignment than pre-aligning each layer to a common sized boundary? (I never paid much attention to this, it always just worked right when doing things manually in Gimp.)

I am a great believer in keep things as simple as possible. You can place images in IM using offsets, not sure how well that works with the IM composite command. An alternative is resize the top/mid images to background size using IM in the script.

Edit:
I had a look at offsets for the composite command and not able to find anything slick. Again this is just an example for a single set of files, add your own loops, variables etc. but for a simple linear sequence this:
Make an empty canvas - add the top image with offsets - add the middle image with offsets - combine with the background. Offsets are top-left-corner of an image.

Code:
magick  -size 500x500 canvas:transparent canvas.miff
magick composite   top.xcf -geometry +80+50  canvas.miff  first.miff
magick composite mid.xcf -geometry +70+250 first.miff  second.miff
magick composite  second.miff  back.xcf combined.png
rm *.miff

Still using the .xcf's from yesterday, the .miff files are IM's own format. Maybe ok if all the offsets are the same, if they vary between sets of images then I think your original is still the easiest way. Gets this https://i.imgur.com/kYUfrW1.jpg dotted in the top/mid image sizes
Reply
#7
Thanks for the follow up Rich.  I have now implemented all your suggestions!
Reply


Forum Jump: