Page 1 of 1

Howto give an image partial transparency

Posted: 2018-04-02T00:10:10-07:00
by phleagol
If I have an image without transparency, then how do I give it partial transparency the PerlMagick way? At the commandline, this can be achieved with something like this...

Code: Select all

convert input.png -alpha set -channel A -evaluate set 50% output.png
So if anyone can show me how to do the same in perlmagick, then I would be most grateful.

Re: Howto give an image partial transparency

Posted: 2018-04-02T06:32:05-07:00
by snibgo
I don't use Perl, but the documentation http://www.imagemagick.org/script/perl-magick.php says there is an "Evaluate()" method. Doesn't that work?

Re: Howto give an image partial transparency

Posted: 2018-04-02T06:59:08-07:00
by phleagol
snibgo wrote: 2018-04-02T06:32:05-07:00I don't use Perl, but the documentation http://www.imagemagick.org/script/perl-magick.php says there is an "Evaluate()" method. Doesn't that work?
Yeah, I saw the Evaluate method, and it looked like the way, but syntax examples were lacking. How does 'evaluate set 50%' translate into working perlmagick? The guesswork perl I've tried so far hasn't worked at all...

Code: Select all

 ##  Read input image.
my $file = "sample.png" ;
open(IMAGE, $img) ;
my $img = Image::Magick->New(quality => 100) ;
$img->Read(file => \*IMAGE) ;
close(IMAGE) ;

##  Half transparency. Still doesn't work :-(
$img->Set( alpha => 'Set' ) ;
$img->Evaluate( channel => 'A', operator => "Set", value => 50 ) ;

##  Save output image.
$img->Write(
        filename => 'out.png', 
        quality  => 100,
    ) ; 

Re: Howto give an image partial transparency

Posted: 2018-04-02T16:26:11-07:00
by phleagol
Okay, I was on the right track. This works better. Thanks.

Code: Select all

$img->Set( alpha => 'Set' ) ;
$img->Evaluate( channel => 'Alpha', operator => "Multiply", value => .5 ) ;