Convert greyscale image to alpha channel (opposite of Separate for channel=>Alpha)

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
jont
Posts: 3
Joined: 2017-09-09T01:28:11-07:00
Authentication code: 1151

Convert greyscale image to alpha channel (opposite of Separate for channel=>Alpha)

Post by jont » 2017-09-09T01:50:29-07:00

I'm trying to extract an alpha channel as a greyscale image, manipulate it, and then either apply the greyscale image to the alpha channel on another image, or use the greyscale image as an alpha mask for a Composite operation.

I'm extracting the alpha channel as a greyscale image with something effectively like:

Code: Select all

    my $alphagreyscale=$original->Clone();
    $alphagreyscale->Separate(channel=>'Alpha');
This works fine, but when I try to use my manipulated greyscale as an alpha mask, something effectively like:

Code: Select all

    my $greenblock=Image::Magick->new();
    $greenblock->ReadImage('xc:green[1000x1000!]');
    my $target=Image::Magick->new();
    $target->ReadImage('canvas:transparent[1000x1000!])';
    $target->Composite(image=>$greenblock, mask=>$alphagreyscale);
The masking happens on a threshold basis, so I only get completely green or completely transparent pixels out - all my hard work on precise alpha levels is lost!

Is there any way I can make the masking happen the way I want, or I can do the opposite of the ->Separate in order to apply my greyscale image as an alpha channel?

Thanks all!

Jont.

snibgo
Posts: 9157
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert greyscale image to alpha channel (opposite of Separate for channel=>Alpha)

Post by snibgo » 2017-09-09T03:17:53-07:00

At the command-line interface, "-compose CopyAlpha -composite" will copy a grayscale image to the alpha channel of an image. Is that helpful? I don't do Perl, but the documentation http://www.imagemagick.org/script/perl-magick.php says Composite can have "compose=>CopyOpacity".
snibgo's IM pages: im.snibgo.com

jont
Posts: 3
Joined: 2017-09-09T01:28:11-07:00
Authentication code: 1151

Re: Convert greyscale image to alpha channel (opposite of Separate for channel=>Alpha)

Post by jont » 2017-09-09T13:54:19-07:00

Thanks for that, but as I understand it, that will copy the alpha mask from one image into another. What I need to do is use a greyscale image as an alpha mask, or is there some trick I'm missing here?

snibgo
Posts: 9157
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert greyscale image to alpha channel (opposite of Separate for channel=>Alpha)

Post by snibgo » 2017-09-09T14:29:21-07:00

For CopyOpacity, see http://www.imagemagick.org/Usage/compose/#copyopacity . It copies either the alpha (if there is one), or the intensity (when there is no alpha, or it is disabled) from one image to another.

The intensity can also be used as a mask for the composite operation. There was a problem with this not long ago; what version IM do you have? Perhaps an upgrade will fix it.
snibgo's IM pages: im.snibgo.com

jont
Posts: 3
Joined: 2017-09-09T01:28:11-07:00
Authentication code: 1151

Re: Convert greyscale image to alpha channel (opposite of Separate for channel=>Alpha)

Post by jont » 2017-09-10T01:17:35-07:00

Yes, I'm just getting a completely opaque image with CopyOpacity. I'm running a very old version of Image Magick currently (6.86), because I've been unable to get a more recent version working with the combination I'm using of XAMPP/Apache/mod_perl/ActivePerl/ImageMagick without having to build my own binaries. I guess I need to try again to get a more recent version running. Might have to resort to building my own binaries, which I have no clue how to do on Windows. :?

Or maybe I can use this problem to convince my client to move the whole project onto Linux. :D

Post Reply