Composite with different transparencies

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?".
weatherbadger
Posts: 15
Joined: 2010-03-01T09:52:50-07:00
Authentication code: 8675308
Location: Oxford UK

Composite with different transparencies

Post by weatherbadger »

Hi

Been a while. Upgraded laptop with Opensuse 11.3 and ImageMagick 6.6.1-0 2010-07-29 and can't now make a good composite of two images:

http://www.weatherbadger.com/Charts/tes ... _rain4.png is the varying transparent base.
http://www.weatherbadger.com/Charts/test/USAsnow4.png has a transparent background and should just fit on top.

I used to use a very simple

Code: Select all

composite USAsnow4.png transp_rain4.png finalimg.png
in my scripts which worked fine! This now seems to screw up the transparency somewhere. I'm not quite sure why - it may be the images themselves are being made slightly differently).

Can anyone suggest a quick fix please?

Cheers
WB
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Composite with different transparencies

Post by GreenKoopa »

weatherbadger wrote: Can anyone suggest a quick fix please?
convert USAsnow4.png transp_rain4.png -background white -flatten finalimg.png

Both images have transparency. What should the final image be where both inputs are transparent? I assumed white above.
weatherbadger
Posts: 15
Joined: 2010-03-01T09:52:50-07:00
Authentication code: 8675308
Location: Oxford UK

Re: Composite with different transparencies

Post by weatherbadger »

Sorry, I realise I wasn't clear, I want the background as transparent. ie the transp_rain4.png with the snow image on top.

-background doesn't have a transparent option¸so how can I ensure this transparency is copied across?
weatherbadger
Posts: 15
Joined: 2010-03-01T09:52:50-07:00
Authentication code: 8675308
Location: Oxford UK

Re: Composite with different transparencies

Post by weatherbadger »

Just to clarify further, the final result should look something like
http://www.weatherbadger.com/Charts/tes ... ecip39.png
where the snow (white to grey) is opaque and the rain (blue) is varying transparency. all on a transparent background...

Cheers
WB
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Composite with different transparencies

Post by GreenKoopa »

What you are doing looks like it should work to me, but I'm not very experienced. The black seem strange. Even stranger, swapping the image order makes the black go away. I do notice that one image uses transparent black, the other transparent white. You can try setting -compose. I'll ponder it.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Composite with different transparencies

Post by anthony »

weatherbadger wrote:-background doesn't have a transparent option¸so how can I ensure this transparency is copied across?
Actually it does... use a background color of 'None' or "Transparent' :-)

However when only two images are bing used and no virtual position offsets, you may as well use -composite instead of -flatten, That is only one compose, when -flatten would be two, with an extra image creation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Composite with different transparencies

Post by anthony »

Looking at the three images, rain, snow, and result, problem is that you want to retain transparency, but you want the 'snow' to completely replace the rain colors.

Questions:
* is the 'snow' always in the same areas as 'rain' as it in on your example images?
* do you want 'gray' areas, rather than say rain with the the snow numbers overlaid
* do you want a mix of the two images where they overlap?

To get what you expect it seems you either want one overlay image, or the other, without any mix between the two images. That seems to be a 'masked' merger.

Take the 'snow' image, and generate a black and white mask of the areas with snow. Blur it a little if you like a smoother transition. then do a three image composition to produce the merged result.
See http://www.imagemagick.org/Usage/compose/#mask
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Composite with different transparencies

Post by GreenKoopa »

GreenKoopa wrote:I do notice that one image uses transparent black, the other transparent white.
I also notice that one image uses and indexed palette and the other doesn't. I don't see that either of these matter though.


Anthony:
anthony wrote:you may as well use -composite instead of -flatten, That is only one compose, when -flatten would be two, with an extra image creation.
Of course. I was trying to work around the unexpected black. What is suppose to happen when someone composites two transparent pixels? weatherbadger is getting black in one case and transparent in another. I don't see why, and he doesn't think he used to get the black.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Composite with different transparencies

Post by anthony »

For any Duff-Porter Alpha composition, transparent colors should never have any effect on the result... period. That is why their is only 12 combinations of color/transparency in the duff-porter set instead of 16.

See...
http://www.imagemagick.org/Usage/compose/#duff-porter

The default compose method 'Over' is a Duff-Porter Compose method.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
weatherbadger
Posts: 15
Joined: 2010-03-01T09:52:50-07:00
Authentication code: 8675308
Location: Oxford UK

Re: Composite with different transparencies

Post by weatherbadger »

Composite worked for me previously and produces the sort of results as per my 3rd link.

In answer to your qus Anthony. Yes, the snow is opaque patches on a transparent background, not necessarily, and I think almost always where the rain is.
Rain is varying levels of transparent-opaque blue on a transparent background.
Not quite sure how you define mix, but as layers I want the snow image to go on top of the rain.

My suspicions are that the chart plotting software which gives a transparent background to the snow image has moved the transparency setting goalposts somehow... GreenKoopa - you said one uses transp white, and one transp black, and one indexed palette - not quite sure how to check that (or what it means!) but perhaps I can change that when the first images are created - that may be up to me then, but perhaps you can advise on how to check the difference between transparent black/white.

Like I said,

Code: Select all

composite USAsnow4.png transp_rain4.png finalimg.png
did work fine for a while so hopefully not too much head scratching required...
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Composite with different transparencies

Post by GreenKoopa »

Anthony:
anthony wrote:For any Duff-Porter Alpha composition, transparent colors should never have any effect on the result... period. That is why their is only 12 combinations of color/transparency in the duff-porter set instead of 16.

See...
http://www.imagemagick.org/Usage/compose/#duff-porter

The default compose method 'Over' is a Duff-Porter Compose method.
Agreed, but I don't see how that explains where the black background comes from. None of the Duff-Porter methods show two transparent pixels turning black.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Composite with different transparencies

Post by anthony »

I doneloaded the snow image and found it has boolean transparency (either on or off, nothing between).

As such over should do the job, an no change should be needed.

the 'rain' image on the other had really does have a scale of transparency.
composite USAsnow4.png transp_rain4.png result.png
works perfectly fine for me.

However a recent change was that IM is attempting to generate smaller PNG pallette image (with multiple levels of alpha). However in this case it is failing to preserve alpha!!!!!

This is a known BUG but you should report this (as a new topic) the BUG forum with pointers to the original image, and probably to this post. Probably notify 'glennrp' of the PNG failure in this case. with a pointer to that BUG topic.

To stop IM from trying to generate 'bad' palette image try doing this instead
composite USAsnow4.png transp_rain4.png png32:result.png
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Composite with different transparencies

Post by GreenKoopa »

Anthony's solution works great!!! I will write up the bug.
weatherbadger wrote: My suspicions are that the chart plotting software which gives a transparent background to the snow image has moved the transparency setting goalposts somehow... GreenKoopa - you said one uses transp white, and one transp black, and one indexed palette - not quite sure how to check that (or what it means!) but perhaps I can change that when the first images are created - that may be up to me then, but perhaps you can advise on how to check the difference between transparent black/white.
This apparently only mattered because of that bug.

> identify USAsnow4.png
USAsnow4.png PNG 600x600 600x600+0+0 8-bit PseudoClass 11c 12.3KB 0.000u 0:00.000
so an 11 color indexed palette

> identify transp_rain4.png
transp_rain4.png PNG 600x600 600x600+0+0 8-bit DirectClass 37.9KB 0.000u 0:00.000

As for the color of the transparency, I opened the images in Paint.NET and used the eyedropper. I'm sure other image editors will work. Transparent pixels can be black, white, any other color, and even vary from pixel to pixel. Sometimes this is for a reason, sometimes not. -alpha background can be used to set the color of transparency.
weatherbadger
Posts: 15
Joined: 2010-03-01T09:52:50-07:00
Authentication code: 8675308
Location: Oxford UK

Re: Composite with different transparencies

Post by weatherbadger »

BUG - oh my, that makes me feel just slightly less stupid than when I normally post here.

"Identify" - very useful.

However, the png32: which works for you but does not for me. Still getting black instead of transparent Hmmm... Is it an IM version thing?
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Composite with different transparencies

Post by GreenKoopa »

I am using version 6.6.6-2 or 6.6.6-3. (seems to be some labeling confusion in this version)
Post Reply