(none) imager-devel
/ help / lists / applications / search /

Re: Compositing images

From: Arnar Mar Hrafnkelsson (
Date: Thu 27 Mar 2003 - 21:02:47 GMT

The general method is along these lines:

1. Load background image
2. Load image to be put on 'top' (this is the transparent one).
3. Use convert() to make an rgba version of the image from step 2.
4. $background->rubthrough(src=>$rgbaimage);

$background will now have $rgbaimage on top of it. The transparency is
controlled by the 4th channel (the alpha) of $rgbaimage. Some image
formats have alpha channel information in them, this is true for png
images at least and Imager can load those directly. In that case you can
skip step 3 and just the image directly because it will already be 4

To make the alpha channel be all a constant you can do something like:

$rgbaimage = $topimg->convert(matrix => [ [1, 0, 0],
                                          [0, 1, 0],
                                          [0, 0, 1],
                                          [0, 0, 0, 0.5] ]);

This will make the alpha channel be a constant 50% transparent. A small
complete example is below:

#!/usr/bin/perl -w

use Imager;

$background = Imager->new();
        or die $background->errstr();

$top = Imager->new();
        or die $top->errstr();

$rgba_image = $top->convert(matrix => [ [1, 0, 0],
                                          [0, 1, 0],
                                          [0, 0, 1],
                                          [0, 0, 0, 0.5] ])
        or die $top->errstr();




If you want to do more fancy things with the alpha channel you can use
$img->getmask()/$img->setmask() to draw only in the alpha channel, see
Imager::ImageTypes for more on that.

-Hope this Helps, Arnar

On Wed, 26 Mar 2003 07765@xyz.molar.is wrote:

> I can't quite figure out how to composite one image on another with a
> degree of transparency. All the "rubthrough" examples seem to show
> how to do it by creating a new image and then drawing on it using a 4
> channel color. I'm thinking that I want to read in two images and
> then draw to the alpha of one of them. Is this the right approach?
> If so, how do you draw on just the alpha? If not, how should I do it?
> Thanks,
> Thad