Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Setting opacity with dissolve in perlmagick

by shohn (Novice)
on May 28, 2012 at 10:23 UTC ( [id://972807]=perlquestion: print w/replies, xml ) Need Help??

shohn has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to overlay 3 images using the Image::Magick module but I'm having trouble achieving the desired result. I have a background image (pure white background created on the fly), a foreground image (a jpeg I read from a file, which I want to overlay onto the background with an opacity of 87.8%) and a mask (a png with transparency) that overlays the whole thing.

From the command line, this is the result I'd like to achieve:

composite -dissolve 87.8 foreground.jpg -size 500x500 xc:white output. +jpg; composite mask.png output.jpg output.jpg
I've written some code in perl using Image::Magick which looks like this:
my $mask = Image::Magick->new; my $background = Image::Magick->new; my $foreground = Image::Magick->new; $mask->Read("mask.png"); $background->Set(size=>'500x500'); $background->Read("xc:white"); $foreground->Read("foreground.jpg"); $background->Composite(compose=>'Dissolve', opacity => '87.8', image=> +$foreground); $background->Composite(image=>$mask); $background->Write("output.jpg");

For some reason the opacity is being ignored (changing the background image from white to red, or blue, has no effect on the image). Any help would be appreciated.

Replies are listed 'Best First'.
Re: Setting opacity with dissolve in perlmagick
by zentara (Archbishop) on May 28, 2012 at 10:32 UTC
    For some reason the opacity is being ignored

    It would be nice if you posted a runnable code example, so we can test. Image and Text Watermarked Letters might have some clues for you. FWIW, PerlMagick's syntax can be tricky. From a quick look at it, compared to my watermark code, it looks like you are missing a % sign.

    opacity => '87.8' should be '87.8%'

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Hi, thanks for the reply. Here's some runnable code (it's the closest I can get to how I need my image to look):

      use Image::Magick; use strict; my $mask = Image::Magick->new(magick=>'png'); my $background = Image::Magick->new(magick=>'png'); my $foreground = Image::Magick->new(magick=>'png'); $mask->Read("png:mask.png"); $background->Set(size=>'500x500'); $background->Read("xc:white"); $background->Set(magick=>'png'); $foreground->Read("foreground.jpg"); $foreground->Set(magick=>'png'); $background->Composite(compose=>'Dissolve', opacity => '87.8%', image= +>$foreground); $background->Composite(image=>$mask); $background->Write("output.png"); exit;

      I've had to go round adding 'png' all over the place in attempt to get it working. The mask can be any png with transparency (any kind of watermark), and foreground any 500x500 image.

Re: Setting opacity with dissolve in perlmagick
by zentara (Archbishop) on May 28, 2012 at 13:07 UTC
    Hi, I think I found the answer for you. Change 'Dissolve' to 'Atop'. The following script works here like the shell version.

    P.S. Don't feel like a dunce, I seem to remember spending a few hours trying every compose type when I discovered this a few years back. :-) I changed a few filenames and lowered the opacity for clarity.

    #!/usr/bin/perl use warnings; use Image::Magick; use strict; #From the command line, this is the result I'd like to achieve: # composite -dissolve 87.8 foreground.jpg -size 500x500 xc:white outpu +t.jpg; # composite mask.png output.jpg output.jpg my $mask = Image::Magick->new(magick=>'png'); my $background = Image::Magick->new(magick=>'png'); my $foreground = Image::Magick->new(magick=>'png'); $mask->Read("png:zzzmask.png"); $background->Set(size=>'500x500'); $background->Read("xc:white"); $background->Set(magick=>'png'); $foreground->Read("zzzforeground.jpg"); $foreground->Set(magick=>'png'); $background->Composite(compose=>'Atop', image=>$foreground, opacity => + '50.8%' ); $background->Composite(image=>$mask); $background->Write("zzzpoutput.png"); exit; # The mask can be any png with transparency (any kind of watermark), # and foreground any 500x500 image.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks a lot :) That worked. I spent quite a lot of time looking for how to achieve this, and only found lots of posts about people complaining that opacity wasn't working in perlmagick! Hopefully people will come across this in google when searching with a similar problem.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://972807]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2024-03-28 22:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found