Make background transparent with image :: Magick and Perl

I built a Perl script that draws a banner using Image :: Magick .

# ... some code my $icon = Image::Magick->new(); $icon->Set(size => '16x16'); $icon->Transparent(color=>'#010101'); $icon->ReadImage($imagepath); $full_image->Composite( # small icon image=>$icon, compose=>'Over', opacity=>'99%', x=>'12', y=>'62', ); # ... some code 

Usually the background of the icon ( #010101 ) should be transparent, but it failed ... any idea why this is not working? Or is it possible that I could use the Transparent Method instead?

Edit: Both images are PNG files. Image::Magick version - 6.7.

+4
source share
2 answers

I have not used this module very much, but I have a few ideas :)

  • You set the opacity of $ full_image to 99%. Don't you want the image to be transparent with a lower value?

  • According to this page , you can call a method called opaque:

    Opaque : color => color name, fill => color name, channel => {All, Default, Alpha, Black, Blue, CMYK, Cyan, Gray, Green, Index, Magenta, Opacity, Red, RGB, Yellow}, invert => {True, False}

  • First I read the image and then I will make a transparent color (just in case):

     $icon->ReadImage($imagepath); $icon->Transparent(color=>'#010101'); 
  • ( Ugly hack ) Run the conversion as a system command on the image, then load it:

     my $icon = Image::Magick->new(); $icon->Set(size => '16x16'); system("convert -transparent '#010101' not_transp.png transp.png"); $icon->ReadImage('transp.png'); $full_image->Composite( # small icon image=>$icon, compose=>'Over', opacity=>'99%', x=>'12', y=>'62', ); 
+2
source

You must tell ImageMagick to use the alpha channel.

On the command line, this will be -alpha On .

0
source

All Articles