Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Recursive image processing (with ImageMagic)

by zentara (Archbishop)
on Nov 23, 2012 at 08:08 UTC ( #1005230=note: print w/replies, xml ) Need Help??

in reply to Recursive image processing (with ImageMagic)

Depending on how you process your images, it may be wise to try and reuse a single IM object. This code shows a possible IM glitch which you may run into. It shows the need to clear out the reusable IM object after every use. However, you may get away with a creation/undef usage within a limited scope, where you create a new IM object for every transformation, then undef it. I think that would slow you down with alot of images, so think about a reusable 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"; }

And here is a very basic File::Find script to process files.

#!/usr/bin/perl use File::Find; $|++; my $path = '.'; my $cmd = 'file'; finddepth (\&wanted,$path); # untested regex my $regex = qr/\Q.png$\E/i; sub wanted { return unless -f; #-d for dir ops or comment out for both if ( /$regex/) { print "$File::Find::name\n" #do your ImageMagick processing here } } __END__

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Recursive image processing (with ImageMagic)
by wvick (Novice) on Nov 23, 2012 at 17:43 UTC

    Thanks very much for the code. Please see my earlier reply about a reservation in my mind about using File:Find for "all files" processing, and the difficulties it causes for mkdir in the output. What do you think?

      It also makes the directory handling more complicated as I need to check or create (regardless) the output folder for and every file created. Unless, I suppose, File:Find returns files in blocks so I can check the last folder created and only mkdir when needed.

      Well if you think about it, you have to test each file and directory to find out if you are in the right one, then if each file in that subdir has a .png ending.

      I think to save cpu cycles, in your case, I would run File::Find with a directory test before the file test. $File::Find::prune = 1 will skip processing any files in that subdir. Something like:

      find sub { # first filter out your directories if (-d && $_ !~ / my_target_dirs_regex / ) { $File::Find::prune = 1; return; } # if you make it to here, you are in your target subdir # now do your ImageMagick processing and mkdir # it has to check each file for a .png ending if (-f && $_ =~ / (.*)\.png$ / ) { # do mkdir stuf # do ImageMagick stuff } # or you could just push the files into an array for later processing # push @goners, $File::Find::name; }, @ARGV;

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1005230]
[Veltro]: erix Corion Ofc it is possible. A while back people thought it was not possible to create an app that can detect what you are listing to.
erix puts the needle in the groove
[marto]: depending on what you qualify to be "a while back" that was true :P
[erix]: probably everything was

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2018-05-24 11:49 GMT
Find Nodes?
    Voting Booth?