Page 1 of 1

Creating and applying an alpha mask.

Posted: 2009-05-07T08:38:22-07:00
by kev51773
Hi, I'm trying to programatically create and apply an alpha mask to an image. I can achieve this in GIMP but I have no idea how to achieve this in imagemagick. Can anyone help out.

Basically I'm doing this to work around a bug with Flash CS4 which fails to anti-alias png exports if you've certain specific features.

The process is as follow

From Flash I export a 24bit (no alpha) png with a white background - White.png
From Flash I export a 24bit (no alpha) png with a black background - Black.png
From Flash I export a 24bit (With alpha) png with a transparent background (Looks crap) - Broken.png

In GIMP I open White.png, add Black.png as a new layer with blending set to 'Difference Mode'
I flatten the image and save it as an alpha mask. - mask.png
I then open Broken.png - remove the alpha, and add mask.png as a new alpha mask.
The results are then perfect.

Could someone point me in the right direction to get started making this work with ImageMagick.

Re: Creating and applying an alpha mask.

Posted: 2009-05-07T10:17:05-07:00
by fmw42
It would really help to see your images, especially as we don't know what GIMP difference does nor what your objective really is for this image. In the mean time, look at

http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/channels/

Re: Creating and applying an alpha mask.

Posted: 2009-05-08T01:10:13-07:00
by kev51773
Here's the images for you to look at, to better explain what I'm trying to achieve.

Here's the original broken output from Flash. This only happens if you output to png with alpha and use paths or bitmap fonts.
Image

Here's my White background png with no alpha. Aliasing works fine here
Image

Here's my Black background png with no alpha. Aliasing works fine here
Image

Here's the mask I create by combining the black and white with a difference blending mode
Image

And here's the end result which I achieve by taking the original broken output and replacing the alpha channel with my newly creating mask.
Image

Now I just need to figure out how to achieve the same result using ImageMagick so that I can automate the process.

Obviously life would be easier if Adobe would just fix the bug :)

Re: Creating and applying an alpha mask.

Posted: 2009-05-08T04:25:46-07:00
by kev51773
I've done it. It wasn't as difficult as I imagined either.

composite -compose difference Black.png White.png Mask.Png

convert Mask.png -negate Mask_Inverted.png

composite -compose CopyOpacity Mask_Inverted.png Broken.png Fixed.png

Cheers all.

Re: Creating and applying an alpha mask.

Posted: 2009-05-08T20:11:03-07:00
by anthony
Your mask make make the whole image semi-transparent. You may need to process your mask a little further. Such as using threshold. or prehaps the very complex and advanced
method given in
http://www.imagemagick.org/Usage/channe ... antialised

The examples before that link also look at using difference image masking.