Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Resizing images with transparent background?

by Anonymous Monk
on Oct 13, 2010 at 21:00 UTC ( [id://865158]=perlquestion: print w/replies, xml ) Need Help??

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

Hello fellow Monks,

I'm using Image::Resize to resize images to fit. It works great unless the image has transparencies (usually gif or png). The result is that the transparent pixels are black. And that is not good. White I can work with, but black no no.

My code is basic:

my $img = Image::Resize->new('file.gif'); #or png my $newImg = $img->resize(150,150); my $imgData = $newImg->gif; #or png open(FH, '>newFile.gif'); binmode FH; print FH $imgData; close FH;

So my question is... what can I do about this black transparencies problem? Can Image::Resize handle something like this? I don't mind using a different module.

Any idea?

Replies are listed 'Best First'.
Re: Resizing images with transparent background?
by Khen1950fx (Canon) on Oct 13, 2010 at 22:04 UTC
    Image::Scale can resize and keep transparency if you use the png format instead of gif. I tried this:
    #!/usr/bin/perl use strict; use warnings; use Image::Scale; my $img = Image::Scale->new('image.gif'); $img->resize_gd( { width => 150 } ); $img->save_png('newImage.png');
      Image::Scale looks great. Can't get it to install though, because I don't have libjpeg and libpng on my XP vm. Working on it.
Re: Resizing images with transparent background?
by BrowserUk (Patriarch) on Oct 13, 2010 at 22:00 UTC

    The problem is that Image::Resizer creates a new default image into which it resizes the original. It takes no account of the attributes--truecolor .v. palette; transparancy color etc.--of the original image.

    It could (should) query these attributes of the original image and use them to create a compatible image before performing the copyResize(). Something like this (untested) might do the trick:

    sub resize { my $self = shift; my ($width, $height, $constraint) = @_; unless ( defined $constraint ) { $constraint = 1; } unless ( $width && $height ) { croak "Image::Resize->resize(): usa +ge error"; } if ( $constraint ) { my $k_h = $height / $self->height; my $k_w = $width / $self->width; my $k = ($k_h < $k_w ? $k_h : $k_w); $height = int($self->height * $k); $width = int($self->width * $k); } my $image = GD::Image->new($width, $height, $self->truecolor ); ## + modified $image->transparent( $self->transparent() ); ## Added $image->copyResampled($self->gd, 0, 0, # (destX, destY) 0, 0, # (srcX, srxY ) $width, $height, # (destX, destY) $self->width, $self->height ); return $image; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thanks for the code. I tried it and perl tells me it can't locate methods "trueColor" and "transparent" ?!? I have no idea how that can be since these methods are part of GD::Image and all the other GD::Image methods work. Any ideas?

        The modified lines should be:

        my $image = GD::Image->new($width, $height, $self->gd->truecolor ) +; $image->transparent( $self->gd->transparent() );

        Still essentially untested. I haven't found an image with a transparent background kicking around.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Resizing images with transparent background?
by pobocks (Chaplain) on Oct 14, 2010 at 01:40 UTC

    Alternately, if you can call out to the shell, you could use imagemagick's "convert."

    system("convert", "-resize", "file.gif", "newfile.gif");

    There are plenty of reasons you might not be able to or want to, but I thought I'd mention this just in case.

    for(split(" ","tsuJ rehtonA lreP rekcaH")){print reverse . " "}print "\b.\n";
      like pobocks says, imagemagick is an alternative. it's a very nice alternative if you ever need to do more, e.g. composite images, many more formats, add text/borders/effects. good fun, and quick.
      the hardest line to type correctly is: stty erase ^H
        I did have reasons not to use ImageMagick, but it works damn well. And pretty simply too. It is a heavyweight though.
Re: Resizing images with transparent background?
by afresh1 (Hermit) on Oct 13, 2010 at 21:24 UTC

    It looks like Image::Resize makes a copy of the image with GD::copyResampled and there the transparency is lost. Unfortunately I don't know enough about GD to know how to fix it.

    l8rZ,
    --
    andrew

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://865158]
Approved by Corion
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found