Composite Overlay 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
rgdonato
Posts: 5
Joined: 2014-07-07T15:07:13-07:00
Authentication code: 6789

Composite Overlay with Transparency

Post by rgdonato »

Hello,

I'm trying to replicate a Gimp overlay, but I'm having problems.
The source image has transparent pixels, it's a 32-bpp PNG.
I'm applying a Overlay of 40% of one color (or a image with this color), but the result transparent pixels are being also affected.

My command-line is:

Code: Select all

convert -alpha Extract image.png \( overlay.png -alpha set -channel A -evaluate set 40% \) -alpha On -compose overlay -composite result.png
And the PHP is:

Code: Select all

  		
$overlay = new Imagick(realpath('overlay.png'));
$overlay->evaluateimage(Imagick::EVALUATE_DIVIDE, 2, Imagick::CHANNEL_ALPHA);
$img = new Imagick(realpath('image.png'));
$img->stripimage();	
$img->compositeimage($overlay, Imagick::COMPOSITE_SCREEN, 0, 0);//, Imagick::CHANNEL_ALPHA);
$img->writeimage('result.png');
$img->destroy();
The source image:
Image

The overlay image:
(Or, it can be used an overlay image with same widrh/height with the color #c44a4a and 40% opacity in the composite)
Image

The final (Gimp) image result that I want:
Image


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

Re: Composite Overlay with Transparency

Post by fmw42 »

I'm applying a Overlay of 40% of one color (or a image with this color), but the result transparent pixels are being also affected.
I am really not sure what you are trying to do. Can you explain more?

If all you want to do is reduce the alpha value, then

reduce to 40% max

Code: Select all

convert image.png -channel a -evaluate multiply 0.4 +channel result.png
reduc by 40%

Code: Select all

convert image.png -channel a -evaluate multiply 0.6 +channel result.png

If this is not what you want, the please give more details. I am not sure where the second image comes into play here, since I see no red mixed with your image?

If you are trying to combine the transparency from the two images and use that for the base image, then

Code: Select all

convert image.png overlay.png \
\( -clone 0,1 -alpha extract -compose multiply -composite \) \
-delete 1 -alpha off -compose copy_opacity -composite result2.png
rgdonato
Posts: 5
Joined: 2014-07-07T15:07:13-07:00
Authentication code: 6789

Re: Composite Overlay with Transparency

Post by rgdonato »

Hello, fmw42.

I'm going to explain what I did in Gimp, I think it can be more useful.
I open the image.png (The first image)
Then I create a layer, with opacity 40%, method: overlay. (The second image is this image, but with 100% opacity)
I fill this layer with the color #c44a4a.
Then I get the final_gimp.png (The 3rd image, that's the result I want to achieve :))

P.S.: The transparent pixels in the source (image.png) is needed in the result image. Like Gimp does.


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

Re: Composite Overlay with Transparency

Post by fmw42 »

Still not sure what is going on. I am not following exactly what you are doing in GIMP. Are you creating a "redish" image with 40% transparency? What blending mode are you using?

But this is what I think you want, though it does not match your GIMP result.

Code: Select all

convert image.png overlay.png \
\( -clone 0 -alpha extract \) \
\( -clone 1 -clone 2 -alpha off -compose copy_opacity -composite -alpha on -channel a -evaluate multiply 0.4 +channel \) \
-delete 1,2 -compose overlay -composite result3.png
Edited to replace flatten with -compose overlay
rgdonato
Posts: 5
Joined: 2014-07-07T15:07:13-07:00
Authentication code: 6789

Re: Composite Overlay with Transparency

Post by rgdonato »

Hello again, fmw42.

Still is not the effect I'm getting in Gimp.
I'm just want to create a new layer, with a color, with overlay and opacity of 40%.
If I made in Gimp, the image result is perfect.
With Adobe Photoshop the transparent pixels from image.png is going to be the same as overlay.png, that I don't want to.

If you see the final_gimp.png (3rd image, result), you wlll notice that the green arc is someway with a little red overlayed on it, but the transparent pixels is not touched.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite Overlay with Transparency

Post by fmw42 »

I have edited my command to use -compose overlay.

I cannot seem to figure out in GIMP how to add the layer as you did. I am not that familiar with GIMP. What menu selections did you use to create the new layer with the 40% transparency and redish color?
rgdonato
Posts: 5
Joined: 2014-07-07T15:07:13-07:00
Authentication code: 6789

Re: Composite Overlay with Transparency

Post by rgdonato »

Ok,

In Gimp I create the layer this way:
Menu Layer -> New Layer
- Layer Fill type: Transparency

Then I got the Bucket Fill Tool, from Menu Tools -> Paint Tools -> Bucket Fill
- Color (HTML) c44a4a (red)
Fill up the entire new Layer that was created
Changed Layer Mode to Overlay (from the Layer widget)
Above the Layer Mode, I changed the Layer Opacity to 40.0%.

A screenshot image from Layer widget to check:
Image

EDIT: By the way, I can use and save the overlay.png, if needed, already with the 40% opacity from Gimp, then the IM convert just need to overlay both images without losing the transparency.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite Overlay with Transparency

Post by fmw42 »

Sorry, but I cannot reproduce your results in GIMP. I have the same screen as you, but my result is not even close to yours.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite Overlay with Transparency

Post by fmw42 »

This is the closest I can come by following your directions but using IM

First line: read image.png
Second line: create your reddish image with 40% opacity
Third line: extract the alpha channel from image.png to use as a mask during overlay
Fourth line: overlay the two images using the mask to preserve the fully transparent areas

Code: Select all

convert image.png \
\( -clone 0 -fill "#c44a4a" -colorize 100 -alpha set -channel a -evaluate set 40% +channel \) \
\( -clone 0 -alpha extract \) \
-compose overlay -composite result4.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Composite Overlay with Transparency

Post by fmw42 »

I processed the image again in GIMP and now get close to your result, but not exactly the same. My result above from IM is quite different from both your GIMP and my GIMP results.

It is possible it has something to do with different profiles?
rgdonato
Posts: 5
Joined: 2014-07-07T15:07:13-07:00
Authentication code: 6789

Re: Composite Overlay with Transparency

Post by rgdonato »

I'm going to check about profiles tomorrow and I will tell you. I'm on Mac right now, so I'm going to try to make this Gimp layer thing in another machine in Windows. Thanks a lot for the help!!

Another quick question, I often use Save for Web from Adobe Photoshop, as PNG-24, sRGB. There's a way to automate this as well in IM?


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

Re: Composite Overlay with Transparency

Post by fmw42 »

rgdonato wrote:I'm going to check about profiles tomorrow and I will tell you. I'm on Mac right now, so I'm going to try to make this Gimp layer thing in another machine in Windows. Thanks a lot for the help!!

Another quick question, I often use Save for Web from Adobe Photoshop, as PNG-24, sRGB. There's a way to automate this as well in IM?


Thanks!
My best regards!

I was using GIMP on my Mac as well.

I do not think IM has any automated Save For Web. I thought that was only for JPG (in GIMP and Photoshop). The only controls for saving as PNG are primarily compression using -quality. See http://www.imagemagick.org/Usage/formats/#png.

I tried adding an sRGB profile to the input image in IM, but it did not make it any closer to GIMP. Something else is different or the overlay methods are different. See http://www.imagemagick.org/Usage/compose/#overlay However, I have no idea what GIMP is doing?

Can you duplicate your GIMP result in Photoshop?

My result matches what I get from Photoshop, with the exception that in PS, the transparent area show partial transparent red. The PS result just needs to be masked by the alpha channel in image.png in order to match my IM result.

So GIMP is doing something odd compared to IM and PS, which is likely the algorithm they use for overlay.
Post Reply