Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

resize image

by esolm (Acolyte)
on Apr 20, 2001 at 22:57 UTC ( [id://74271]=perlquestion: print w/replies, xml ) Need Help??

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

I have a bunch of images that I want to resize is there easy way to do this

Replies are listed 'Best First'.
Re: resize image
by Masem (Monsignor) on Apr 20, 2001 at 23:01 UTC
    You absolutely need an external library to do this since perl doesn't handle graphics natively. The best one out there is Image::Magick which are perl functions that access the ImageMagick library of graphics routings including resizing images and so forth.
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain

      Will Image::Magick work with GraphicsMagick? I found no Graphics::Magick on the CPAN.

(Vortacist) Re: resize image
by Vortacist (Pilgrim) on Apr 21, 2001 at 00:14 UTC

    Well, you could take a look at shrink.pl. It's a little command-line utility script that recurses the directories you give it and will scale images by whatever amount you specify. (It happens to be my first Perl Monks post. ;) If you want an example of how to use Image::Magick, it might help.

Re: resize image
by Chady (Priest) on Apr 20, 2001 at 23:03 UTC

    a quick search at the box above, showed this.

    use Image::Magick


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
Re: resize image
by Beatnik (Parson) on Apr 21, 2001 at 02:51 UTC
    AFAIK GD does resizing too, altho loads less than Image::Magick.

    Greetz<BR Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: resize image
by sindicalista (Initiate) on Mar 15, 2014 at 08:35 UTC

    I found using Imager and Imager::File::JPEG the easiest solution. Works on Ubuntu, and I assume other Linux flavors, not on Windows

    here's a simple script that iterates the .jpg files in a directory and resizes them.

    use strict; use warnings; use Imager; use Imager::File::JPEG; my $folder = shift; my $dir = "/media/mark/My Passport/archive"; #starting point $dir = $dir . '/'. $folder; #target subdirectory chdir($dir); my $dh; opendir($dh, $dir); while(readdir($dh)){ my $fn = $_; next unless $fn=~ m/\.jpg$/; my $newimg; print "checking $fn\n"; my $img=Imager->new(file=>$fn,type=>'jpeg'); my ($height, $width); $height = $img->getheight; $width = $img->getwidth; my $orient = 'Portrait'; #default if ($height < $width){ $orient = 'Landscape'; } print "processing $fn height->$height width=$width orient = $orien +t\n"; if($orient eq 'Portrait' && $height < 800){ $newimg = $img->scale(ypixels=>900); } elsif($orient eq 'Landscape' && $width < 1200){ next if ($height / $width) >= 0.75; $newimg = $img->scale(xpixels=>1200); } else{ #dimensions OK, nothing to do next; } $height = $newimg->getheight; $width = $newimg->getwidth; print "saving $fn orientation->$orient, width->$width height->$hei +ght\n"; $newimg->write(file=>$fn, type=>'jpeg',jpegquality=>100) or die "cannot save altered image $img->errstr"; + }

      Good work!. I'll suggest to catch both .jpg and .JPEG files (lower and uppercase) changing the 13th line to:

      next unless $fn=~ m/\.jpe?g$/i;

      You can also save a couple of lines

      my ($height, $width) = ($img->getheight,$img->getwidth);
Re: resize image
by hawtin (Prior) on May 31, 2008 at 05:32 UTC

    This topic has been discussed many times before, the simple search box can quickly find everything you need, for example pick any one of:

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-23 12:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found