Page 1 of 1

Problem with compositing/masks

Posted: 2007-11-06T11:38:56-07:00
by Ryland
My webhost updated to the latest version of ImageMagick a few days ago (6.3.6) and this caused a bunch of my ImageMagick perl scripts to break. Before, I was able to composite using a greyscale PNG as an alpha mask; specifically, I would allow the user to choose a color, I would create an image handle and fill it with that color, create another image handle and load the PNG image into it, and composite the filled image onto a third base image using the PNG as a mask:

Code: Select all

// create the base image;
$base = Image::Magick->new;
$base->Set(size=>"$imgsize");
$base->Read("xc:#$imgbgcolor");

// create the 'fill' image
$tints{"$bgcolor"} = Image::Magick->new;
$tints{"$bgcolor"}->Set(size=>"$imgsize");
$tints{"$bgcolor"}->Read("xc:#$bgcolor");

// create the mask image
$bgmask = Image::Magick->new;
$bgmask->Set(size=>$imgsize);
$bgmask->Read("images/art-web/bg-fill.png");

// composite the 'fill' image onto the base image using the PNG as a mask
$base->Composite(image=>$tints{"$bgcolor"},compose=>'Over',mask=>$bgmask,x=>0,y=>0,gravity=>'NorthWest');
Then the image will be output as a GIF, and it'll appear as the shape in the PNG image composited onto the background image in the user-specified color. After the upgrade, it appears as a single-color image, in the user-specified color, as if the mask didn't work (or was all white). However (and this is the oddest part) if I resize $base before I output it as a GIF, it looks fine (albeit at a different size). If I output it as a PNG instead of as a GIF, it looks fine. But if I output it as a GIF, it's a solid color. Did something about alpha compositing or outputting to an 8-bit graphic change in the latest version?

Re: Problem with compositing/masks

Posted: 2007-11-06T11:41:10-07:00
by Ryland
Also: this only happens if I use a PNG image as a mask; if I create an image and use that as a mask (say, by creating an image handle, filling it with black, and then drawing white text on it), it works as before.

Re: Problem with compositing/masks

Posted: 2007-11-06T11:52:30-07:00
by Ryland
Also: if you want to see this problem in action, go to:

http://www.says-it.com/seal/circle.php

Enter some text and click "Go", and you'll be treated to a lovely olive-drab GIF. But if you change the output size (there's a drop-down right above the "Go" button), it looks fine.

Re: Problem with compositing/masks

Posted: 2007-11-08T13:38:28-07:00
by Ryland
I figured it out myself (as usual):

Code: Select all

// create the base image;
$base = Image::Magick->new;
$base->Set(size=>"$imgsize");
$base->Read("xc:#$imgbgcolor");

// create the tint image
$tints{"$bgcolor"} = Image::Magick->new;
$tints{"$bgcolor"}->Set(size=>"$imgsize",matte=>'True'); # have to set matte to True
$tints{"$bgcolor"}->Read("xc:#$bgcolor");

// create the mask image
$bgmask = Image::Magick->new;
$bgmask->Set(size=>$imgsize);
$bgmask->Read("images/art-web/bg-fill.png");

// add PNG to my tint image as an alpha channel
$tints{"$bgcolor"}->Composite(image=>$bgmask, compose=>'CopyOpacity');

// composite the tint image onto the base image normally
$base->Composite(image=>$tints{"$bgcolor"},compose=>'Over',x=>0,y=>0,gravity=>'NorthWest');
I've almost got the generator working again, but it's not quite there yet.

Re: Problem with compositing/masks

Posted: 2007-11-19T18:11:59-07:00
by anthony
You could have tried the IM Examples pages for an answer too.

Sorry I couldn't respond quicker. Been very busy with improvements for resizing and distortions.

Re: Problem with compositing/masks

Posted: 2007-11-19T19:14:11-07:00
by Ryland
I did read the IM Examples page, didn't find an answer to my question. I found a clue in an old IM mailing list that turned up in a Google search, and managed to figure out the rest.

Your example pages are comprehensive, but they don't address the PerlMagick API. I use PerlMagick much more than I use command line IM, and the IM command line switches and parameters doesn't always correspond with the Perl object method/property equivalents. I'm getting a much better handle on PerlMagick these days, though.

And I apologize for being a bit snippy, but my Perl scripts were working fine, and then a slightly newer version of IM came out, and then my scripts didn't work any more. If it were a major revision, I would accept it as a matter of course that there would be syntax changes, but apparently it was just a patch to fix a security bug, and suddenly my scripts are broken. I don't understand why an API function call would change, at least not in a minor revision. You'll have to forgive me if I find that a bit frustrating.