John Smith
John Smith

Reputation: 1233

overlay images as transparent layers with imagemagick

I want to stack several images using imagemagick. The result I want is the same as I get when I import all images as layers into Gimp and set the layer transparency to some value.

Each image is transparent with a circle in the center of various sizes. Overlaying all N images with a 100/N% opacity should give me something like a blurry blob with radially increasing transparency. Here are three example images.

However if I try to do this with imagemagick, I get a black background:

convert image50.png -background transparent -alpha set -channel A -fx "0.2" \( image60.png -background transparent -alpha set -channel A -fx "0.2" \) -compose overlay -composite -flatten result.png

Edit: After Mark Setchells latest comments, I got

What I want is that those areas that appear in all images (the center in the example) add up to to non-transparent region, while those regions that appear only on fewer images get more and more transparent. Marks example seems to work for 3 images, but not for a larger stack. The result I would like to get would be this one (here I emphasize the transparent regions by adding a non-white background):

The example images are made from this one

using this bash command:

for i in $(seq 10 10 90); do
    f="image$i.png"
    convert https://i.sstatic.net/hjWgF.png -quality 100 -fuzz $i% -fill white -transparent black $f
done

Upvotes: 2

Views: 7258

Answers (2)

Caleb
Caleb

Reputation: 5428

What you need here is a different mode of compositing. You were using -compose overlay which is going to lighten up the result with each successive layer. What you probably want here is -compose blend to keep only the most saturated value or or just -compose over to layer them with no modifications.

Upvotes: 1

Mark Setchell
Mark Setchell

Reputation: 207465

#!/bin/bash

# Calculate how many images we have
N=$(ls image*.png|wc -l)
echo N:$N

# Generate mask, start with black and add in components from each subsequent image
i=0
convert image10.png -evaluate set 0 mask.png
for f in image*png;do
   convert mask.png \( "$f" -alpha extract -evaluate divide $N \) -compose plus -composite mask.png 
done

# Generate output image
convert image*.png      \
   -compose overlay -composite \
   mask.png -compose copy-opacity -composite out.png

Upvotes: 1

Related Questions