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

As best I understand it, the Imager set of modules should support image transparency. However, I can't get the results I would expect from the following snippet:

use Imager; $img = Imager->new(xsize=>700, ysize => 500, channels => 4); # setting Imager::Color->new($red, $green, $blue, $alpha); $blue = Imager::Color->new( 0, 0, 255, 255); $red = Imager::Color->new( 255, 0, 0, 63); $img->box(color => $blue, xmin=>10, ymin=>30, xmax=>200, ymax=>300, fi +lled=> 1); $img->box(color => $red, xmin=>50, ymin=>80, xmax=>250, ymax=>350, fil +led=> 1); my $outfile = "foo.png"; $img->write(file=>$outfile) or die $img->errstr;

I'd expect the resulting image to contain a blue rectangle partly covered by a semitransparent red rectangle, such that the area covered by both shapes shows as purple.

Instead, I get the blue rectangle partly offset from and completely obscured by a pale red rectangle, with nothing like what I'd call transparency.

Am I using Imager::Color incorrectly? How should transparency work?

Replies are listed 'Best First'.
Re: Creating transparent images using Imager
by Corion (Patriarch) on Apr 27, 2009 at 11:16 UTC

    I don't think that Imager will mix the images according to their transparency as long as you use the built-in commands. I think if you want proper bitmap mixing according to the transparency, you need to draw the bitmaps into separate Imager canvasses and then overlay the two canvasses.

      Thanks... if you know how, could you perhaps express "draw the bitmaps into separate Imager canvasses and then overlay" as a few lines of Perl?
        I've never used Imager before :) This seems to work
        #!/usr/bin/perl -- use strict; use warnings; use Imager; # setting Imager::Color->new($red, $green, $blue, $alpha); my $blue = Imager::Color->new( 0, 0, 255, 255); my $red = Imager::Color->new( 255, 0, 0, 63); my $canvas_blue = Imager->new(xsize=>700, ysize => 500, channels => 4) +; $canvas_blue->box(color => $blue, xmin=>10, ymin=>30, xmax=>200, ymax= +>300, filled=> 1); my $canvas_red = Imager->new(xsize=>700, ysize => 500, channels => 4); $canvas_red->box(color => $red, xmin=>50, ymin=>80, xmax=>250, ymax=>3 +50, filled=> 1, alpha => 1); my $outfile = "foo.png"; # overlay $canvas_blue->rubthrough( src => $canvas_red ); $canvas_blue->write(file=>$outfile) or die $canvas_blue->errstr;
        canvas, overlay, alpha, Imager::Transformations
Re: Creating transparent images using Imager
by tonyc (Hermit) on Apr 28, 2009 at 00:20 UTC

    Imager's default combining mode is "none" - which replaces the target pixel, you can supply a fill object (or get Imager to make one) that uses the "normal" mode to get the transparency effect you're after:

    use strict; use warnings; use Imager; my $img = Imager->new(xsize=>700, ysize => 500, channels => 4); # setting Imager::Color->new($red, $green, $blue, $alpha); my $blue = Imager::Color->new( 0, 0, 255, 255); my $red = Imager::Color->new( 255, 0, 0, 63); $img->box(color => $blue, xmin=>10, ymin=>30, xmax=>200, ymax=>300, filled=> 1); $img->box(fill => { solid => $red, combine => 'normal' }, xmin=>50, ymin=>80, xmax=>250, ymax=>350); $img->write(file => 'transbox.png') or die;