Search code examples
perlperlmagick

Add an alpha channel to an image read in with PerlMagick


I'm reading in image that doesn't have an alpha channel:

my $image = Image::Magick->new;
$image->Read("./noalpha.png");

And then trying to set certain pixels to a different color/alpha value:

my @color = ( 0.2, 0.4, 0.6, $alpha );
$image->SetPixel( x=>$X, y=>$Y, channel=>'RGBA', normalize=>'True', color => \@color);

But unless the starting image file already had an alpha channel the file I write:

$image->Write('out.png');

doesn't contain an alpha channel.

I've been reading through the PerlMagick documentation, but I must not be looking for the right thing. Is there a way to add an alpha channel to my $image object?

Do I need to create a new image object with the sizing of the original image and re-write everything to that one?


Solution

  • The existence of an alpha channel is an attribute of the image, which needs to be turned on:

    $image->Set(alpha => 'On');