Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: thumbnails generator

by zentara (Archbishop)
on Apr 20, 2008 at 16:42 UTC ( [id://681802]=note: print w/replies, xml ) Need Help??


in reply to thumbnails generator

I know you admit it's not the most efficient, but if you had alot of thumbs to convert, I would think you would be better off doing it all in Perl, and not calling convert thru system for each image. The ImageMagick object can be created once, cleaned out of data, and reused for the next image. I havn't benchmarked it, but I think it would be much more efficient that way. For anyone looking, it would be done something like this, to reuse a single IM object.
#!/usr/bin/perl use warnings; use strict; use Image::Magick; my $image = Image::Magick->new; umask 0022; my @pics= <*.jpg>; #my @pics= <*.jpg *.gif *.png>; #add all your extensions here foreach my $pic (@pics){ my ($picbasename) = $pic =~ /^(.*).jpg$/; my $ok; $ok = $image->Read($pic) and warn ($ok); my $thumb = $picbasename . '-t.jpg'; $image->Scale(geometry => '100x100'); $ok = $image->Write($thumb) and warn ($ok); undef @$image; #needed if $image is created outside loop print "$pic -> $thumb\n"; }

I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Replies are listed 'Best First'.
Re^2: thumbnails generator
by oko1 (Deacon) on Apr 20, 2008 at 17:06 UTC

    Now there is a good reason to do it in Perl! I don't think it would be much slower than a script, but that might be worth testing.

    #!/bin/bash shopt -s nullglob for image in *.{gif,bmp,jpg,wmf,jpeg,png} do printf "Converting $image\n" bits=(`/usr/bin/identify -format "%h %w" "$image"`) if [ ${bits[0]} -gt ${bits[1]} ] then sizes=(600x800 81x108) else sizes=(800x600 108x81) fi ext=${image##*.} /usr/bin/convert -resize ${sizes[0]}! "$image" "${image/.$ext/-l.$ +ext}" /usr/bin/convert -resize ${sizes[1]}! "$image" "${image/.$ext/-s.$ +ext}" done

    Update: Fixed a few subtle problems in the script that surfaced with more extensive testing (not that anybody will care, this not being Perl - but I do. :)

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    

Log In?
Username:
Password:

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

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

    No recent polls found