http://qs321.pair.com?node_id=473274

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

I figured that I wanted a way to batch process images. Since I made the move to Linux, I lost some of my favorite Windows only tools. So I decided to utilize Perl and Gimp as a substitute.
Checking into the Gimp documents I found that Gimp has procedures that can be called from Perl to take advantage of Gimps code. Long story short, I've been banging my head against Gimps poor documentation. At this point, I'm simply frustrated with Gimp and the Perl interface. I spent nearly ten hours just trying to locate and install all the separate Perl modules that I needed to interface with Gimp. To make matters worse, Gimp IRC couldn't point me in the right direction since they've never even heard of the procedures. Code samples and tutorials are just vague enough that moving what I learn to different functions falls flat on their face.

I'm fed up with Gimp. I've decided that Gimps interface documentation simply isn't mature enough to even bother with anymore. So I ask this of you my friendly Monks. Does anyone know how to utilize Gimps Perl interface effectively? Is there a different module to manipulate the images the way I want?

My Current Goal:
I have a collection of images for my website. One of my goals is to assist in changing the colors of the images by replacing pixels of one color with another. The images are stored as needs dictate such as .gif, .jpeg, or .png. I know how to use Perl to filter out the files I don't want altered.
Another, related goal, is rotating an image a set number of degrees and saving each rotation as a different file.

----
Thanks for your patience.
Prove your knowledge @ HLPD

Replies are listed 'Best First'.
Re: Batch image editing
by shiza (Hermit) on Jul 07, 2005 at 22:57 UTC
Re: Batch image editing
by grantm (Parson) on Jul 08, 2005 at 01:05 UTC

    As shiza commented, Image::Magick might fit the bill. I have recently been playing around with Imager and found it to have a much cleaner API.

    I have also built an experimental gear for Sprog to do an image transformation. My vision there is to have a bunch of image processing gears that each do one specific transformation and can be chained together if necessary. The one I've built, adds a border around the image. The user gets to select border width and colour via a properties dialog. Other ones I have in mind are thumbnail generation, rotation, adding text labels (eg: copyright info or captions) and auto cropping. Imager can be used for all those things.

Re: Batch image editing
by mugwumpjism (Hermit) on Jul 08, 2005 at 04:38 UTC

    Just today I was looking at this little Gimp-Perl plug-in I wrote, so that I could instantly save my .XCF gimp working files as .png with a single hotkey.

    It's a very basic example, I guess to apply it to your problem you'd want to call the plug-in-color-map function (found in the DB Browser), uncomment the undo group bits and so on. Then just stick it in /usr/lib/gimp/2.0/plug-ins (or wherever your plug-ins directory is).

    As others have pointed out, ImageMagick is normally the way to go for this sort of thing. But of course, gimp offers a much richer image manipulation toolkit, so mastering the gimp is a worthwhile goal.

    use Gimp; use Gimp::Fu; #Gimp::set_trace(TRACE_ALL); register "pngify", "Save this image as a PNG", "Save this image as a PNG, so that you don't have to go throu +gh million s of dialogs.", "Sam Vilain", "Sam Vilain", "1.0", N_"<Image>/File/Save as PNG", "RGB*, GRAY*", [ ], sub { my($img,$drawable)=@_; my $fn = $img->get_filename; $img = $img->duplicate; #eval { $img->undo_group_start }; $drawable = $img->flatten; $fn =~ s{\.\w+}{.png} or die; $img->file_png_save_defaults($drawable, $fn, $fn); #eval { $img->undo_group_end }; undef; }; exit main;
    $h=$ENV{HOME};my@q=split/\n\n/,`cat $h/.quotes`;$s="$h/." ."signature";$t=`cat $s`;print$t,"\n",$q[rand($#q)],"\n";
Re: Batch image editing
by monarch (Priest) on Jul 08, 2005 at 05:30 UTC
    This may not necessarily be the angle you're looking for, but I've written a few batch-imaging tools using Perl and the NetPBM toolkit. Essentially NetPBM is a set of command-line utilities that do one thing and one thing only at a time on your image. Essentially you a)convert your image from .gif/.jpeg/.png to a common internal format (.pbm), then b)perform an operation such as recolouring, rotating, then c)convert the common internal format (.pbm) back into a .gif/.jpeg/.png. Oh, and there's also a Win32 port of the NetPBM toolkit.

    Another approach I took was to code using C and statically linking with the GD ToolKit. No doubt another approach is using the Perl GD library.

    Just food for thought.

      The problem with converting to a common format and back again is that for lossy compression schemes (i.e. JPEG), you will lose image quality each time you do this. It's very much like the old "generation problem" with analouge audio recordings: the more copies you make, the more loss you have, and the poorer the quality.

      Your first run or two is fine, but after a few runs, you will likely notice some image degradation.

      Larry Wall is Yoda: there is no try{}
      The Code that can be seen is not the true Code
        That is true, except that it has nothing to do with converting to a common format. If you have a jpg, and open it in gimp, make some changes, and then save it again, your image quality will degrade. The only solution is to always store your images in a lossless format, and only consider jpgs to be the "end result." Much like a programmer would never delete the source code after compiling, don't delete the original RAW, or TIFF, or whatever after creating a jpg.
Re: Batch image editing
by zentara (Archbishop) on Jul 08, 2005 at 11:16 UTC
    Gimp is a great program, but the perl interface to it is lousy. Use the Imager module for your ideas above. For a sample of what Imager can do, see Tk ImageMap-color-zones

    I'm not really a human, but I play one on earth. flash japh
Re: Batch image editing
by Anonymous Monk on Jul 08, 2005 at 00:01 UTC
Re: Batch image editing
by steven.franklin (Initiate) on Jul 08, 2005 at 21:28 UTC
    This may be of some use to you:

    http://www.imagemagick.org/script/perl-magick.php

    http://www-128.ibm.com/developerworks/library/l-graf/?ca=dnt-428

    http://www-128.ibm.com/developerworks/library/l-graf2/?ca=dgr-lnxw15GraphicsLine



    Book:

    Graphics Programming with Perl ISBN 1930110022
Re: Batch image editing
by mrpeabody (Friar) on Jul 10, 2005 at 03:22 UTC
    For an un-Perlish solution, try nconvert. It's a command-line image munger; it reads and writes everything and does pretty much everything except for adding text. It is cross-platform and free for non-commercial use.

    link