On Thu, Sep 06, 2018 at 04:58:04AM -0400, Michele Beltrame via RT wrote:
Show quoted text> When doing something like this on a transparent PNG:
>
> use Imager;
> use strict;
>
> my $img = Imager->new( file => './fsc.png' );
> $img = $img->convert( preset => 'noalpha' );
> $img = $img->scale( xpixels => 100 );
> $img->write( type => 'jpeg', file => './fsc.jpg' );
>
> the background becomes black (curiously, it is white if I don't resize the image).
> I know I can create a new white image and compose the PNG into it. However, since most of the time what one needs is white background, could maybe an option be added to allow for it i.e.:
>
> $img = $img->convert( preset => 'noalpha', background => Imager::Color->new("#FFFFFF") );
Thanks for the ticket.
If you only need to add the white background because you're saving to
JPEG, do you realize you can supply a background colour to write()?
$im->write(file => "./fsc.jpg", i_background => Imager::Color->new("#FFF"));
The operation you're asking for does seem useful, but it doesn't
belong in convert().
The problem with compose() for your case is:
- you can just supply a background color, you need to make a new image
- you don't get that nice functional flow of out = in->operation()
So I expect I'd add:
$out = $im->compose_over(color => ...);
and maybe later add:
$out = $im->compose_over(fill => ...);
$out = $im->compose_over(image => ...);
and they all return a new image instead of modifying any inputs.
Finally, convert(preset => "noalpha") just strips the alpha channel,
so a pixel #FF0000 pixel with 25% coverage in the source image with
alpha, becomes a full #FF0000 pixel in the output rather than the
expected #3f0000 you'd expect against a black background.
To demonstrate this, try creating a transparent image in Gimp and use
the airbrush to draw on it a little in red. If you save that as a PNG
and then use Imager with convert(preset => "noalpha"), you'll see what
I mean.
Tony