Problem with compositing/masks

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
Ryland

Problem with compositing/masks

Post 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?
Last edited by Ryland on 2007-11-07T20:00:47-07:00, edited 1 time in total.
Ryland

Re: Problem with compositing/masks

Post 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.
Ryland

Re: Problem with compositing/masks

Post 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.
Ryland

Re: Problem with compositing/masks

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problem with compositing/masks

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Ryland

Re: Problem with compositing/masks

Post 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.
Post Reply