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

creating thumbnails

by jwlarson3rd (Acolyte)
on Dec 16, 2003 at 22:45 UTC ( [id://315163]=perlquestion: print w/replies, xml ) Need Help??

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

I am creating a image gallery using perl and mysql. I found the following script to create thumbnails of images in a directory. It executes but doesn't work. I want to put this snippit in the gallery display script(which works)to create thumbnails of files that are not already created. I don't understand why this doesn't work. I suppose that I could be haveing a path problem but I don't think so but I have tried http://typhoon.he.net/~bthcraft/john/images
/home/bthcraft/public_html/john/images
/home/~bthcraft/john/images
any help would be appreciated john larson
#!/usr/bin/perl -w my $size=100; my $qual=50; my $prefix="tn_"; $dir="/home/bthcraft/public_html/john/images"; opendir(DIR,"$dir"); my @files=grep(/\.jpeg$/,readdir(DIR)); closedir(DIR); foreach $file (@files) { system("convert -geometry ".$size."x".$size."-quality $qual $file".$prefix."$file"); }

Replies are listed 'Best First'.
Re: creating thumbnails
by Zaxo (Archbishop) on Dec 16, 2003 at 23:06 UTC

    You don't check the exit status, $?, after the system call. That would tell you what the error is - it's almost surely there since you say the perl program executes without complaint. Odds are that convert (part of ImageMagick) is not installed on the server, or not in your path. Here, it's at /usr/X11R6/bin/convert.

    It is good practice to use the list form of system,

    my $result = system '/path/to/convert', '-geometry', "${size}x${size}", '-quality', $qual, $file, "tn_${file}"; die $? if not defined $result;
    The concatenation in your example may lack needed whitespace, which the list form takes care of.

    Update: If you have a lot of images it would be worthwhile to write a makefile and put the thumbnails in a subdirectory. As written, your script will write thumbnails of thumbnails each time it is run, as well as redoing work it has already done.

    After Compline,
    Zaxo

Re: creating thumbnails
by b10m (Vicar) on Dec 16, 2003 at 23:17 UTC
    Maybe not what you're looking for, but why not use Image::Magick, or better yet Image::Thumbnail instead of the system call? AFAIK, `convert` uses the ImageMagick libs too.
    --
    b10m
Re: creating thumbnails
by r11132a (Scribe) on Dec 16, 2003 at 23:12 UTC
    John,

    While it would be more helpful, if you could post what errors, if any,
    you are getting, right off the top of my head I would suggest you take
    a look at your system() call.
    system("convert -geometry ".$size."x".$size."-quality $qual $file".$prefix."$file");
    Now I'll give you that you probably split the line to make it easier to
    read in your post, however, the statement is probably not passing a
    valid command to the shell. For example, if
    $file = "someimage.jpeg";
    at some point in the loop, then the system() will be called with the string
    convert -geometry 100x100-quality 50 someimage.jpegtn_someimage.jpeg
    Check your spacing and see if that fixes your problem.
Re: creating thumbnails
by zentara (Archbishop) on Dec 17, 2003 at 17:53 UTC
    Here is a nice thumbnail creating script for jpgs.
    #!/usr/bin/perl # by Merlyn use strict; use Image::Magick; my $im = Image::Magick->new; umask 0022; my @names = @ARGV ? @ARGV : grep { -f and -B } <*>; for (@names) { if (/ /) { my $old = $_; tr, ,_,s; $_ .= ".jpg" unless /\.jpg$/; !-e and rename $old, $_ or next; warn "renaming $old to $_\n"; } next if /\.thumb\.jpg$/; my $thumb = "$_.thumb.jpg"; next if -e $thumb; undef @$im; my $ret; $ret = $im->Read($_) and warn($ret), next; $ret = $im->Scale( geometry => '100x100' ) and warn($ret), next; $ret = $im->Write($thumb) and warn($ret), next; warn "thumbnail made for $_\n"; }
      Sorry Merlyn, I don't claim posted code is my own, I just save and catagorize any useful script I see. This is one of the best.
        Hi! is there a way to read / get thumbnails from jpegs? I think exif thumbnail, because loading a large image may take a lot of time. and if there is already a thumbnail stored in the exif then it might greatly lessen the time of create a thumbnail.
Re: creating thumbnails
by flyingmoose (Priest) on Dec 17, 2003 at 16:33 UTC

    Though igal does not use a database back-end, it is Perl, and it rocketh. The gallery pages are exceptionally good to look at.

    It seems that you might just be reinventing a wheel or two! Like your program, igal also uses the ImageMagick tools. It does not use the aforementioned Perl modules, of course, which I would consider an improvement. The igal author probably didn't consider adding a local copy of the "non-standard" modules in question, or didn't know that was possible. I would at least consider reviewing the source, even if you can't use the full program.

    Igal @ stanford.edu

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://315163]
Approved by Itatsumaki
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: (5)
As of 2024-04-24 06:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found