Howto give an image partial transparency

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
phleagol
Posts: 9
Joined: 2017-12-16T02:20:37-07:00
Authentication code: 1152

Howto give an image partial transparency

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Howto give an image partial transparency

Post 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?
snibgo's IM pages: im.snibgo.com
phleagol
Posts: 9
Joined: 2017-12-16T02:20:37-07:00
Authentication code: 1152

Re: Howto give an image partial transparency

Post 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,
    ) ; 
phleagol
Posts: 9
Joined: 2017-12-16T02:20:37-07:00
Authentication code: 1152

Re: Howto give an image partial transparency

Post 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 ) ;
Post Reply