PNGs as layers with different opacities

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
Kjuuze
Posts: 3
Joined: 2016-03-29T05:24:27-07:00
Authentication code: 1151

PNGs as layers with different opacities

Post by Kjuuze »

Hi there

Let's say I have the following images:
- a.png
- b.png
- c.png

They're all of the same size and have an alpha channel. Now i need to combine them to one image (as layers one on top of the other) with different opacity values (a.png 0.9, b.png 0.5, c.png 0.6). The new image should be a transparent png too.
In PS I would make a new image, add three layers for the images and set the layer opacity. But with IM I didn't get the result i need. Not even after a couple of hours googling...

Here an example of what I intend to do:
Image

Some help would be much appreciated, thx a lot!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PNGs as layers with different opacities

Post by Bonzo »

What version of IM are you using and what command did you try?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNGs as layers with different opacities

Post by fmw42 »

In the future, please identify your IM version and platform and provide separate images for us to test. I have not taken the time to try to separate your images, so this is untested and could be order dependent. But it looks like this should do what you want if I have the order correct.

Unix syntax:

Code: Select all

convert \
\( b.png -alpha set -channel a -evaluate set 0.5 +channel \) \
\( a.png -alpha set -channel a -evaluate set 0.9 +channel \) \
\( c.png -alpha set -channel a -evaluate set 0.6 +channel \) \
-flatten PNG32:result.png
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: PNGs as layers with different opacities

Post by GeeMack »

Kjuuze wrote:Now i need to combine them to one image (as layers one on top of the other) with different opacity values (a.png 0.9, b.png 0.5, c.png 0.6). The new image should be a transparent png too.
I'm using Windows 7 64, PhotoShop Elements 8.0, and "ImageMagick 6.9.3-6 Q16 x64".

I made a 3 layer image in PS using your "a.png", "b.png", and "c.png". I set the "a.png" layer to 90% opaque, the "b.png" layer over it to 50% opaque, and the "c.png" layer over that to 60% opaque. I merged down the layers and saved it as a PNG image to compare.

Then I did this "convert" command with IM...

Code: Select all

convert ^
   -background none ^
   -compose blend ^
   ( -define compose:args=90 ( a.png -channel a -evaluate set 0 ) a.png -composite ) ^
   ( -define compose:args=50 ( b.png -channel a -evaluate set 0 ) b.png -composite ) ^
   ( -define compose:args=60 ( c.png -channel a -evaluate set 0 ) c.png -composite ) ^
   -compose over ^
   -flatten output.png
The results were virtually identical to the PS process. The "convert" command goes something like this...

Start by setting the background color as "none".

Set the compose method to "blend".

Set the blend level at 90% opaque. Create a transparent image from the "a.png" and blend composite "a.png" over it.

Set the blend level at 50% opaque. Create a transparent image from the "b.png" and blend composite "b.png" over it.

Set the blend level at 60% opaque. Create a transparent image from the "c.png" and blend composite "c.png" over it.

Set the compose method to "over".

Flatten the three images into a single PNG named "output.png".

If you're on a *nix OS you'll probably have to escape those parentheses with a backslash "\" and replace the continued line carets "^" with a backslash "\" too. I'm not sure if it'll need a couple other syntax tweaks, but that should get pretty close.
Kjuuze
Posts: 3
Joined: 2016-03-29T05:24:27-07:00
Authentication code: 1151

Re: PNGs as layers with different opacities

Post by Kjuuze »

Hi,
thx all for your replies!
In the future, please identify your IM version and platform and provide separate images for us to test.
I'll do so in future, thx for your hint! Platform is Unix and IM Version is 6.7.7-10.

With solutions provided like the one from "fmw42" i tested a lot and got everytime just a blank white output file.
Great thanks to GeeMack for his detailed solution. The result is nearly perfect the one i expected! :D Like he mentioned for Unix Platforms the command looks as followed:

Code: Select all

convert \
   -background none \
   -compose blend \
   \( -define compose:args=90 \( a.png -channel a -evaluate set 0 \) a.png -composite \) \
   \( -define compose:args=10 \( b.png -channel a -evaluate set 0 \) b.png -composite \) \
   \( -define compose:args=60 \( c.png -channel a -evaluate set 0 \) c.png -composite \) \
   -compose over \
   -flatten output.png
I would have been lost without you! IM is so powerful but hard to master! Thx again for the great and fast help and have a nice day!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PNGs as layers with different opacities

Post by fmw42 »

try

Code: Select all

convert -background none \
\( a.png -alpha on -channel a -evaluate multiply 0.9 +channel \) \
\( b.png -alpha on -channel a -evaluate multiply 0.5 +channel \) \
\( c.png -alpha on -channel a -evaluate multiply 0.6 +channel \) \
-flatten PNG32:result1.png
Kjuuze
Posts: 3
Joined: 2016-03-29T05:24:27-07:00
Authentication code: 1151

Re: PNGs as layers with different opacities

Post by Kjuuze »

Thxs for your reply again. Your solution seems to work just perfect for my case. And it's a little bit less complicated than the previous solution. Thumbs up!!
Post Reply