Stacking images with transparency

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
lugiber
Posts: 5
Joined: 2013-10-21T09:05:12-07:00
Authentication code: 6789

Stacking images with transparency

Post by lugiber »

I have four images that i need to merge/stack to one image. But for the life of me i can't get image magick to do what i want.

Basically i want a background image with 100% opacity then i want to add another image on top of it with 70% opacity, then another image with 40% transparency and finally at the very top one with 10%. Thereby merging all four images to one.

I hope i have made myself clear. All the images have the same pixel dimensions.

Any imput would be very appreciated!

/Peter
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Stacking images with transparency

Post by fmw42 »

If the images already have the needed transparency, then

convert background image1 image2 image2 -background none -flatten result

If the images do not have transparency, then

convert background \
\( image1 -alpha set -channel a -evaluate set 70% +channel \) \
\( image2 -alpha set -channel a -evaluate set 40% +channel \) \
\( image3 -alpha set -channel a -evaluate set 10% +channel \) \
-background none -flatten result
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Stacking images with transparency

Post by anthony »

Fred. didn't you arrange to add a 'weighted average' type operator. Hmmm.... -poly
That would be perfect for this. I'm surprised you didn't mention it.

Mind you your are overlaying (equivalent to -dissolve), rather than adding (equivalent to -blend, -average, and -poly) so I suppose your solution makes more sense.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
lugiber
Posts: 5
Joined: 2013-10-21T09:05:12-07:00
Authentication code: 6789

Re: Stacking images with transparency

Post by lugiber »

Thanks Fred!
That worked great!
If you don't mind i'd like to pick your brain one last time.

1.) Would it be possible to save the four images as a layered psd?
2.) Would it be possible to update the transparency of those layers with image magick after the file is saved?

It would be nice to have all the images as editable layers so that i can change the layer transparency after the files are merged without having to rewrite the entire file.
That would save some io on the harddrive.

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Stacking images with transparency

Post by fmw42 »

just try saving to PSD format without the -flatten. That should work but I have not tested.

convert background \
\( image1 -alpha set -channel a -evaluate set 70% +channel \) \
\( image2 -alpha set -channel a -evaluate set 40% +channel \) \
\( image3 -alpha set -channel a -evaluate set 10% +channel \) \
result.psd
Post Reply