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

Re: Background colour of rotated images

From: Tony Cook (
Date: Tue 27 Apr 2004 - 00:46:30 GMT

  • Next message: Andrew Green: "Re: Background colour of rotated images"

    On Mon, Apr 26, 2004 at 08:07:18PM +0100, Andrew Green wrote:
    > Hi,
    > I've just tried out Imager as a replacement for Image::Magick, and I'm
    > really impressed. I'm stuck with one (hopefully minor) problem,
    > however.
    > In rotating an image by a couple of degrees, and then writing the
    > result out as a JPEG, the extra pixels around the edge of the new,
    > rotated image are set to black. I really need to be able to set them
    > to a custom background colour, and I'd rather not have to achieve this
    > by using a fountain fill.
    > Here's a sample of the code I'm using:
    > my $orig = Imager->new();
    > $orig->open(file => $filename) or die $orig->errstr();
    > my $rotated = $comp->rotate(degrees => 2);
    > $rotated->write(file => $filename, type => 'jpeg')
    > or die $rotated->errstr();
    > Would it be possible to set the default background colour of $rotated
    > to a named Imager::Color object? If so, how?

    Currently you can't do this directly, but it's a good idea. Given the
    structure of the transformation code it shouldn't be a big deal to add

    You should be able to do it indirectly using rubthrough():

      # add an alpha channel if there isn't one
      my $fourchan = $comp->convert(preset=>'addalpha');

      # rotate with the alpha
      my $rotate_alpha = $fourchan->rotate(degrees=>2);

      # make a base image to rub into, with out desired $background
      my $out = Imager->new(xsize=>$rotate_alpha->getwidth(),
      $out->box(color=>$background, filled=>1);

      # rub the rotated image through to it
      $out->rubthrough(src=>$rotate_alpha, tx=>0, ty=>0);

      $out->write(file=>$filename, ...) or die;

    I haven't tested this, but I hope it gives you some ideas of how to
    make it work.