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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

I have a vested interest in your question as I have a B&W photography background, and frequently convert color images to grayscale (I'm not British... :). Given this, perhaps the following will assist you--at least to start:

use strict; use warnings; use File::Find::Rule; use File::Basename; use File::Path qw/mkpath/; use List::Util qw/sum/; use GD; my $sourceDir = 'A'; my $destinDir = 'B'; -d $sourceDir or die 'Error: Source directory does not exist.'; !-d $destinDir or die 'Error: Destination directory already exists.'; my @files = File::Find::Rule->file()->name(qr/\.png$/i)->in($sourceDir +) or die 'No png files found in source directory.'; print "\nNumber of png files to convert to grayscale: ", scalar @files +, "\n\n"; for my $pngFile (@files) { my $destinPath = $pngFile =~ s{^$sourceDir(?=/)}{$destinDir}r; my $dir = dirname($destinPath); if ( !-d $dir ) { mkpath( $dir, { error => \my $err } ); !@$err or die qq{Unable to create directory "$dir"}; } print "Processing: $pngFile"; my $image = GD::Image->new($pngFile); for my $i ( 0 .. $image->colorsTotal() - 1 ) { my @gray = ( ( sum $image->rgb($i) ) / 3 ) x 3; $image->colorDeallocate($i); $image->colorAllocate(@gray); } open my $fh, '>', $destinPath or die $!; binmode $fh; print $fh $image->png; close $fh; print " - Done!\n"; } print "\nJob completed.\n";

Sample output when running:

Number of png files to convert to grayscale: 6 Processing: A/adelaide-rosella.png - Done! Processing: A/frog.png - Done! Processing: A/C/chicken_profile.png - Done! Processing: A/C/tux.png - Done! Processing: A/C/D/crowned_crane.png - Done! Processing: A/C/D/cuckoo.png - Done! Job completed.

There's likely a more efficient way to do this, but it worked well in my tests--although I'm not processing millions of images. You'll notice that I used GD for the actual image processing. If you're keen on using ImageMagick, you could just modify this script for it.

It will preserve the structure of the source directory, mirroring it in the destination directory, and convert all found png files to grayscale, and then write them into their destination directory. There's an initial check for the destination directory already existing, since files might otherwise be overwritten.

Hope this helps!

In reply to Re: Recursive image processing (with ImageMagic) by Kenosis
in thread Recursive image processing (with ImageMagic) by wvick

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (4)
    As of 2018-01-22 03:05 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (231 votes). Check out past polls.