Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Trying to learn how to do thumbnails in perl

by wageslave (Novice)
on Apr 12, 2002 at 02:46 UTC ( #158475=perlquestion: print w/replies, xml ) Need Help??
wageslave has asked for the wisdom of the Perl Monks concerning the following question:

Well at this point I'm trying to make a web page for all my pics, so I figure I first have to reduce the size of all the pic and then make thumbnails, so far I've tried gd which doesnt support more then 255 colors, so I tried image-magick but it dies telling me there was a system error, oh yeah I'm using nt and activestate, image_magick dies every time I try to open a pic of any type

so here's the question is there any other way to reduce the over all size of pictures and make thumbnails using perl?

Edit kudra, 2002-04-16 Changed title

  • Comment on Trying to learn how to do thumbnails in perl

Replies are listed 'Best First'.
Re: trying to learn more perl
by talexb (Canon) on Apr 12, 2002 at 02:58 UTC
    I'd recommend merlyn's page here for some good information on that challenge, although I don't know if those tools also run on NT.

    BTW, what errors are you getting? That kind of information would probably be useful if you're expecting mere mortals to help you with your problem. :)

    --t. alex

    "Here's the chocolates, and here's the flowers. Now how 'bout it, widder hen, will ya marry me?" --Foghorn Leghorn

      its a nt error basically a window pops up and says an aplication errors has occured where it promptly kills perl the bottom of the window it say exception access error at some memory location
        In my experience, ImageMagick works very well on NT, but it has some quirks, especially when using it in perl. So, while you might have other options, here are some questions that may or may not help you... :)
        1. Can you do the command directly to the ImageMagick binary (convert.exe) and make it work? If not, download and install again.
        2. Are you using the perl module Image::Magick? Don't. In my experience, it works horribly, if at all, at least in a Windows environment. Try completing step one, then use system calls instead or qx//.
        3. Are you using complete paths? ImageMagick is pretty picky about this, I seem t recall - both complete path to the binary itself and the images to operate on is (or was) needed, I think.

        All in all, IM is a pretty good tool for scripting image conversions and stuff, and it can do most things you need, so maybe you want to give it another shot. On the other hand, I'm sure there are lots of as good or better alternatives... just haven't used them myself. :)

        I also realize that this is a pretty non-perl answer, and that it is even maybe a bad answer considering you wanted to learn more perl, but one single tool is seldom enough, you need a good solid toolbox with compatible tools to do some real work. :)

        You have moved into a dark place.
        It is pitch black. You are likely to be eaten by a grue.
      thanks for all the help I figured out what the prob was, it was an incorrect filehandle, guess what happened was I cut pasted a snippet of someone else code and forgot about the file handle, when I ran the code theres was an excpetion error which basically killed perl, I figured it was the image::magick module that was cause the prob oh well guess a good nights sleep can do wonders for spotting bugs
Re: trying to learn more perl
by jlongino (Parson) on Apr 12, 2002 at 16:31 UTC
    I have had quite good success using ImageMagick along with the Image::Magick module (on both Win98 and Win2K). I suspect that you probably don't have it installed properly. ar0n has posted a program to do what you want and there is a link to merlyn as well in the thread creating thumbnails. I took a little from each and wrote my own for the very purpose you mentioned:
    use strict; use Image::Magick; # Allowable image suffixes my %IMsuffixes = ( jpg => 1, bmp => 1, gif => 1, jpeg => 1, png => 1, tiff => 1, tif => 1, wpg => 1 ); my $max_dim = 125; my $path = 'C:/My Files/My pictures/Pets'; my @images = glob "'$path/*'"; print "Path: '$path'\n"; foreach (@images) { s!.*/!!; next if lc substr($_, 0, 3) eq 'tn.'; my $suf = $_; $suf =~ s/.*\.//; if (exists $IMsuffixes{$suf}) { ThumbNail($path, $_, "tn.", $max_dim); } else { print "INVALID suffix: '.$suf', file: '$_' Skipping . . .\n"; } } sub ThumbNail { my ($path, $file, $prefix, $max_dim) = @_; if (! ($path && $file && $prefix && $max_dim)) { print "ERROR - Invalid Parameter to sub 'ThumbNail': \n", " path => '$path\n'", " file => '$file'\n", " prefix => '$prefix'\n", " max_dim => '$max_dim'\n\n"; exit; } my $img = new Image::Magick; my $imerrmsg = $img->Read("$path/$file"); Process_IM_Error($imerrmsg) if $imerrmsg; my ($width, $height) = $img->Get("width", "height"); if ($width * $height == 0) { print "ERROR - Image: '$path/$file' dimensions must be nonzero: +\n", " width => '$width'\n", " height => '$height'\n\n"; exit; } my ($new_height, $new_width); if ($width > $height) { $new_width = $max_dim; $new_height = int($height * $new_width / $width); } else { $new_height = $max_dim; $new_width = int($width * $new_height / $height); } $imerrmsg = $img->Resize( width => $new_width, height => $new_height, blur => 1 ); Process_IM_Error($imerrmsg) if $imerrmsg; $img->Write("$path/$prefix$file"); print pack("A48", "$prefix$file"), " W => ", pack("A3", $new_width), " H => ", pack("A3", $new_height), "\n"; } sub Process_IM_Error { my $errmsg = shift; print "Made it to Process_IM_Error sub\n"; print shift, "\n"; if ("$errmsg") { $errmsg =~ /(\d+)/; exit if ($1 >= 400); } }


Re: trying to learn more perl
by lshatzer (Friar) on Apr 12, 2002 at 16:29 UTC
    I had this same problem. What I had done was using PPM installed ImageMagick's package, and it worked great, then I downloaded the stand alone version, the exe's and dll's, and for some strange reason, perl would start segfaulting when I used the module. I then removed the stand alone version of ImageMagcik, and kept the module on, and it started to work again.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://158475]
Approved by mdillon
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2018-06-22 19:48 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.