Skip Menu |

This queue is for tickets about the Imager CPAN distribution.

Report information
The Basics
Id: 127047
Status: open
Priority: 90/
Queue: Imager

People
Owner: Nobody in particular
Requestors: arthas [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Allow to set white background when removing alpha channel
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") );
CC: ;
Subject: Re: [rt.cpan.org #127047] Allow to set white background when removing alpha channel
Date: Thu, 6 Sep 2018 20:44:35 +1000
To: Michele Beltrame via RT <bug-Imager [...] rt.cpan.org>
From: Tony Cook <tony [...] develop-help.com>
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
Hello Tony! Thank you for the detailed reply and explanation. I actually wasn't aware of the i_background write() option, which I see is in the documentation but I overlooked it. Indeed, a compose_over(color => ...) method would me a nice addition. Thanks again, Michele.