Page 1 of 1

Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-29T08:48:06-07:00
by Nadav
I am extracting the alpha channel from PNGs with the following command (ver 6.7.6-5 on Win7 64 bit):

Code: Select all

convert Alpha.png -channel Alpha -level 50% -separate "Alpha_mask.bmp"
If the PNG contains an alpha channel it is extracted correctly, with white pixels where the mask is transparent (and the original image should be displayed).
Image ==> Image

But if the PNG does not have an alpha channel, I expect the extract to be entirely white (as the entire image should be displayed).
But it is exported as black :(
Image ==> Image (expected white Image)

Can anyone suggest how to extract a white mask when the PNG does not have an alpha-channel?
Or, how to convert only the all-black images to white (and leaving the images with white&black as they are)?

Thanks!
Nadav

Re: Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-29T10:59:23-07:00
by fmw42
It is possible this is a bug. Extracting the alpha channel seems to be getting the opacity channel and not the transparency channel. I thought it was switched a while ago, but I could be wrong.

This will work as it is now. Whether it is a bug or I am miss-remembering is to be determined.


convert Alpha.png -channel a -negate -threshold 0 -separate Alpha_alpha2.png


Alpha.png -background lightblue -flatten Alpha_blue.png
convert Alpha_blue.png -channel a -negate -threshold 0 -separate Alpha_blue_alpha2.png


See my bug report at viewtopic.php?f=3&t=20876

Re: Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-29T13:56:58-07:00
by fmw42
I believe I am wrong about this being a bug. I think using -channel a (or -channel o) -separate gives the opacity data which is the opposite (-negate) of the transparency data

My solution is correct

convert Alpha.png -channel a -negate -threshold 0 -separate Alpha_alpha2.png

But the better way to get the alpha data directly is

convert Alpha.png -alpha extract Alpha_alpha2.png

convert Alpha.png -background skyblue -flatten Alpha_blue.png
convert Alpha_blue.png -alpha extract Alpha_blue_alpha2.png

Re: Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-29T18:57:37-07:00
by anthony
For some time -channel A actually produces 'matte' values (zero=opaque) in IMv6.

See older example in...
http://www.imagemagick.org/Usage/masking/#matte

Code: Select all

  convert  moon.png   -channel matte -separate  moon_matte.png
In IMv5 you would have needed to use this (awkward) technique to get the 'matte' channel

Code: Select all

     convert moon.png matte:moon.matte
     convert MIFF:moon.matte moon_matte2.png

YES these example uses 'matte' instead of 'a' or the channel setting but that is for clarity. In the channel setting matte and alpha flags are the same, and their is no way for operators to distinguish between them.

This was why -alpha extract was originally added to IM.


However IMv7, with its use of actual alpha values in memory storage, would invert the result of
-channel A -separate. However you should still use -alpha extract when that is what you want.


As such It is probably time for -channel A -separate to be fixed, and produce alpha channel results, BUT scripts that used that now very old technique WILL BREAK.

Update: This has already been done!... That is -separate output alpha, not matte channel images only.

The choice then comes does to, breaking very old scripts and do the right thing, or preserve backward compatibility.

OR, add some quick hack to the IMv6 'channel flags' to allow -separate to distinguish between a user asking for the obsolete -channel o and the newer -channel a setting.

That is -channel rgbo produces different results to the more normal -channel rgba setting, when used with -separate (and only with -separate!)

Re: Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-29T19:44:42-07:00
by fmw42
Update: This has already been done!

The choice then comes does to, breaking very old scripts and do the right thing, or preserve backward compatibility.

OR, add some quick hack to the IMv6 'channel flags' to allow -separate to distinguish between a user asking for the obsolete -channel o and the newer -channel a setting.

That is -channel rgbo produces different results to the more normal -channel rgba setting, when used with -separate (and only with -separate!)
What has already been done. The switch between -channel a -separate and -channel o -separate? Was this done in IM 6.7.6.8 svn? I asked we hold off until we hear from you and we confirm that it has always produced the opacity contrary to my original post.

If that is the case and it has now been changed in IM 6.7.6.8 svn, then perhaps it should be backed out until this is discussed further.

If we proceed anyway, then I would really like to be able to still do -channel o -separate and get the opacity values, even though I would need to change some scripts. Though that is not a big deal as only about a dozen scripts are affected. Eventually (for IM7?) it would likely be needed anyway. However, I am not sure what to do about using -channel rgba, since I had used that primarily to enable all the channels for example when doing

convert logo: -channel rgba -fill none -opaque white logo_trans.png

That should still work. The only time I see an issue would be when doing -channel rgba -separate vs -channel rgbo -separate. That makes sense. But then what would happen to the above example vs

convert logo: -channel rgbo -fill none -opaque white logo_trans.png

should they not do the same thing -- namely just enable the transparency/opacity? If not, then what would the correct thing be in older versions vs newer versions? I would need to know how to change many more scripts for this kind of thing.

Re: Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-29T20:05:01-07:00
by anthony
I clarified. -separate only output alpha channel images now.

The 'OR' alternative was an idea for an hack for IMv6 only. IMv7 is alpha - period! if you want matte you would need to invert.


As for your scripts. I think you can have them use -alpha extract I don't believe their are any packages with a IM old enough to not have the -alpha operator. But you never know. IMv5 stuck around for a long time.

Perhaps we need a list of IM versions, and the last date it was 'seen in the wild'. That will get us an idea of when we can start 'disregarding' older versions of IM. The bugs forum can make a good source for 'you should upgrade' bugs.

Re: Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-29T21:08:19-07:00
by fmw42
The bugs forum can make a good source for 'you should upgrade' bugs.
Yes, I use that as much as possible to set my IM version traps in my scripts. But it is not complete about all changes. That is why I have had to ask at times when something was changed.

Re: Alpha Channel Extract of Non-Alpha PNG

Posted: 2012-04-30T00:21:08-07:00
by Nadav
Thank you for your quick help. -alpha extract does the job