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

Hi all.

This has been a *very* stressful week. So to entertain myself I was thinking about creating a perl program that would enable one to create a banner ( or poster ) based on an image you already had ( or could make ). Here it is:
#!/usr/bin/perl -w use strict; use GD; my $order_of_mag; print "Enter order of magnitude for new picture: "; chomp( $order_of_mag = <STDIN> ); my $pic = GD::Image->newFromPng( "pc.png" ); my @size = $pic->getBounds(); my @newSize = map( { $_ * $order_of_mag } @size ); my $newPic = new GD::Image ( @newSize ); $newPic->copyResized( $pic, 0, 0, 0, 0, @newSize, @size ); open FH, ">pc2.png" || die "Could not write to file: $!\n"; binmode FH; print FH $newPic->png(); close FH or die "Could not close file: $!\n"


If you set the order of magnitude quite high, simply click on the little orange box that appears ( in IE ) then scroll back and forth. Since it writes the new image to a different file, the original won't be disturbed.
Any suggestions on how to improve it are most welcome.

Thanks,
-Katie.

Replies are listed 'Best First'.
Re: Resizing an image with perl.
by io (Scribe) on Aug 12, 2002 at 16:33 UTC

    This line may be a problem:

    open FH, ">pc2.png" || die "Could not write to file: $!\n";

    As the '||' operator has a high predence, perl will try to compare the string ">pc2.png" instead of the return value of the open statement.

    You us the 'or' operator instead, or use brackets like open (FH, ">pc2.png") || die.

    But the rest of your code looks fine, using strict and warnings and such :-)

Re: Resizing an image with perl.
by seattlejohn (Deacon) on Aug 12, 2002 at 17:29 UTC
    Nitpick: You're misusing the term "order of magnitude". An order of magnitude is a 10x difference, two orders of magnitude is a 100x difference, and so on. To perform as advertised, your map should really look something like this:
      map {$_ * 10**$order_of_magnitude} @size;

    But I suspect that's not what you intended, and the term you meant to use was something like "factor".

Re: Resizing an image with perl.
by hossman (Prior) on Aug 13, 2002 at 00:59 UTC
      I am happy to say, that is not true.

      I think that you need to read:
      http://www.boutell.com/gd/manual2.0.1.html
      "...version 2.0 does include ... both truecolor and palette images, resampling (smooth resizing of truecolor images) and so forth. "
      After you have updating your gd library you should go and visit Lincoln Stein's space on cpan to download GD-2.01.
      http://search.cpan.org/author/LDS/

      I had been using GD-2.00 since the 6th with decent results and was very pleased to see GD-2.01 made available.
      Thanks Lincoln!
      An excellent module.

      Mmmm... 16,777,216 colors for me.

      ImageMagick is huge and slow. I was using shell tools (djpeg, etc) simply to avoid it.