1
votes

I want to get the mean of a sequence of images by using Imagemagick. Therefore I use the following command:

convert *.png -evaluate-sequence mean MEAN.png

Each of my images does contain an alpha channel. What I want is: Combine all the images by ignoring the alpha channel.

When I combine the images, the alpha channel is considered in the "mean" method and my final image has transparency. That isn't what I want.

Result:

mean transparency

I tried to add the parameter -alpha off, but then Imagemagick converts the alpha channel to black.

convert *.png -alpha off -evaluate-sequence mean MEAN.png

Result:

mean with alpha off

Photoshop does it right. I load all images in a stack and create a smart object. When I use the "mean" method in Photoshop, the alpha channel is not considdered in the final result.

Result that I want with Imagemagick:

mean with photoshop

Does someone have an idea how to do that with Imagemagick?

4
Not sure what you mean. Try adding -channel RGB before -evaluate-sequence.Mark Setchell
Aah! I can see the images now! It works fine with version 7.x of ImageMagick. Are you running an old version... identify -version will tell you.Mark Setchell
My version is Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-03-14 imagemagick.org -channel RGB wont workluggi
I think that version is several years old now - probably time to upgrade.Mark Setchell
I tried -channel RGB with IM version 7. The result is a black imageluggi

4 Answers

3
votes

What you need to do is to use the alpha channels as weighting images for each image. The total fraction of white values at each pixel from all the alpha channels would be the weight to use for the average. So something like this should do what you want or at least be close.

First, cd to your directory of images. Then run the following.

convert *.png -evaluate-sequence mean \
\( -clone 0 -alpha off \) \
\( -clone 0 -alpha extract \) \
-delete 0 +swap -compose divide -composite result.png

This will work if there is some image texture at each pixel coming from al least one image. That is at a given pixel all images are not totally black (transparent).

compare -metric rmse result.png mean_photoshop.png null:
125.167 (0.00190993)

So this shows that there is about 0.2% difference between my result and what you got from photoshop

2
votes

Maybe this way of working will help you get there - or at least explain the problem:

convert xc:"rgba(255,0,0,1)" xc:"rgba(0,0,0,1)" xc:"rgba(0,0,0,0)" -depth 8 -evaluate-sequence mean txt:

Output

# ImageMagick pixel enumeration: 1,1,65535,srgba
0,0: (21845,0,0,43690)  #550000AA  srgba(85,0,0,0.666667)
1
votes

Using IM 6.8.9.4 Q16 or IM 7.0.5.5 Q16 Mac OSX Sierra, this seems to work fine for me:

Make transparent image

convert logo: -transparent white logot.png

Get mean

convert logot.png logot.png logot.png -alpha off -evaluate-sequence mean result.png

magick logot.png logot.png logot.png -alpha off -evaluate-sequence mean result.png

This also seems to work:

convert logot.png logot.png logot.png -channel rgb -evaluate-sequence mean -alpha off result.png

So perhaps you need to upgrade your ImageMagick (and/or libpng?)

Can you post a zip file of some of your input images, so we can test with your images?

0
votes

One problem that I see is that the PNG images that you provided have black under the transparent areas and not image texture. So when you disable alpha as in my commands above, you see black and the black gets averaged into the final result. Did you use these same PNG images in Photoshop or did you have Photoshop PSD images or some other images that you used and then exported to PNG, which may have put black under the transparent areas. Have you tried using the same PNG images in Photoshop to do the average?

In fact, you have 8-bit color (palette) images, which have one color (black) assigned to be the transparent color.