http://www.perlmonks.org?node_id=164987

#!/usr/bin/perl =head1 NAME gimp-ahsv - Apply "Autostetch HSV" effect on a number of JPEG images =head1 SYNOPSIS gimp-ahsv [options] FILE... Options: -?, --help --man -v, --verbose =head1 OPTIONS =over 4 =item B<-?> =item B<--help> Print a brief help message and exits. =item B<--man> Prints the manual page and exits. =item B<-v> =item B<--verbose> This option increases the amount of information you given during program run. By default, gimp-ahsv works silently. =head1 DESCRIPTION This program applies "Image/Colors/Auto/Autostetch HSV" effect from gimp in batch mode on a number of JPEG images. This effect often improves contrast of pictures and improves their overall look. =head1 BUGS AND LIMITATIONS Loading and saving JPEG files decreases quality of the images. For the purposes I use this script such quality drop is acceptable. If it is not acceptiable for you than use other formats. You should replace file_jpeg_load/file_jpeg_save method calls with other methods from file_xxx_load/file_xxx_save family if you want to use this script with other formats. Unfortunately even in batch mode gimp requires X server so it is impos +sible to run this script without it. =head1 COPYRIGHT Copyright (c) 2002 Ilya Martynov. All rights reserved. This program is free software. It may be used, redistributed and/or modified under the terms of the Perl Artistic License. =head1 SEE ALSO L<Gimp> PDB explorer in gimp =cut use strict; use warnings; use Pod::Usage; # configure Getopt::Long to parse argument string in GNU style use Getopt::Long qw(:config gnu_getopt); my @files; my $verbose; BEGIN { # Gimp module insists on parsing @ARGV on its own; for this reason # argument string is parsed here my %options; GetOptions(\%options, 'help|?', 'verbose|v+', 'man'); pod2usage(-exitval => 0, -verbose => 2, -output => \*STDOUT) if $options{man}; pod2usage(-exitval => 0, -output => \*STDOUT) if $options{help}; @files = @ARGV; pod2usage(2) unless @files; $verbose = $options{verbose} || 0; } use Gimp; use Gimp::Fu; # init Gimp modules Gimp::set_trace(TRACE_ALL) if $verbose > 1; Gimp::init; for my $file (@files) { print "Loading $file ...\n" if $verbose > 0; my $img = eval { Gimp->file_jpeg_load($file, $file) }; if($@) { # catch file loading errors and skip to the next file warn $@; next; } print "Applying effect ...\n" if $verbose > 0; my $drawable = $img->active_drawable; $img->plug_in_autostretch_hsv($drawable); print "Saving $file ...\n" if $verbose > 0; my $quality = 0.8; my $smoothing = 0; my $optimize = 1; my $progressive = 1; my $comment = ''; my $subsmp = 0; my $baseline = 1; my $restart = 0; my $dst = 2; eval { Gimp->file_jpeg_save($img, $drawable, $file, $file, $quality, $smoothing, $optimize, $progressive, $comment, $subsmp, $baseline, $restart, $dst); }; if($@) { # catch file saving errors and skip to the next file warn $@; next; } # free memory $img->delete; }
Update: I forgot to put Gimp->file_jpeg_save into eval. Fixed.

--
Ilya Martynov (http://martynov.org/)

Replies are listed 'Best First'.
Re: Batch conversion of images using gimp and Perl
by Anonymous Monk on Aug 12, 2002 at 02:40 UTC
    Excellent perl script. Works like a charm and does a great job: jpeg file size went down without any noticeable quality degradation. One request though: Would it be possible to add an image re-size option please?
      You can find names of methods that correspond to various Gimp operations via Gimp's PDB Explorer (menu Xtns/PDB Explorer). In this case it is gimp_image_scale. According docs avialable via PDB Explorer this function expects following parameters:
      In: IMAGE image INT32 new_width INT32 new_height
      Perl module Gimp that provides bindings allows you to call this function as
      Gimp->gimp_image_scale($img, $new_width, $new_height);
      It also does some magick so you can call same operation under different names. All following calls do the same thing:
      Gimp->gimp_image_scale($img, $new_width, $new_height); Gimp->image_scale($img, $new_width, $new_height); $img->scale($new_width, $new_height);
      All you have to do is adding such call before saving jpeg file.

      You can learn more from documenation in Gimp module and from Gimp-Perl web site.

      --
      Ilya Martynov (http://martynov.org/)