[magick-users] a nrebie question - how to batch process a list of images

zentara zentara1 at sbcglobal.net
Sun Dec 21 14:09:05 PST 2008


On Sun, 21 Dec 2008 20:52:16 +0000 (GMT)
phsieh2005 at optonline.net wrote:

>Dear IMers:I am wondering if imagemagick will let me achieve what I am trying to do:I have a list of images, such as, A0001.png, A0002.png, A0003.png .. and B0001.png, B0002.png, B0003.png...etc.The A*.png and B*.png has the same number of images, say 200 images.  Actually, each image is 10 seconds apart.  So,1.
>I would like add annotation to each image with the correct time.  For
>example, A0001.png will get "Time: 10 seconds" and A0002.png will get
>"Time: 20 seconds" ... etc.2. After this, I would like to
>combine each A's and B's into C's.  For example, A0001.png + B0001.png
>=> C0001.png, and A0002.png + B0002.png => C0002.png .... etc.The main difficulty I have is: how to batch processing all 200 images.Thanks!Pei

Sounds like an easy job for Perl, while watching football. :-)
It's alot easier for me than bash shell.

Here are 2 scripts, the first one will make 20 white and black
test files. Run this file once to make test files if needed.

The second script will read them in and montage them according to
the number.

###############  make some test files if needed ################3
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;

# first make some test files for testing
 my $image = Image::Magick->new(magick=>'png');
 $image->Set(size=>'200x200');

foreach my $x(0..19){
   my $rc = $image->Read("xc:white");

  $image->Annotate(
           stroke    => 'black',
           pointsize => 24,
           fill      => 'black',
          text      => "Time : $x",
          x         => 50,  
          y         => 60,
         );
    
  my $name = 'A'.$x.'.png';
  $image->Write($name);
  undef @$image;
}

foreach my $x(0..19){
   my $rc = $image->Read("xc:black");

  $image->Annotate(
           stroke    => 'white',
           pointsize => 24,
           fill      => 'white',
          text      => "Time : $x",
          x         => 50,  
          y         => 60,
         );
    
  my $name = 'B'.$x.'.png';
  $image->Write($name);
  undef @$image;
}

exit;
###################################################

###########  do the actual combining ##################
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;

# first make an IM objects for efficient reuse
my $image = Image::Magick->new(magick=>'png');

 # maybe better method than reading twice, but this is a start
 my $dir;
 opendir($dir, '.') or die("ack: $!\n");
 my @afiles = grep /^A\d+\.png$/, readdir $dir;
 closedir $dir;

 opendir($dir, '.') or die("ack: $!\n");
 my @bfiles = grep /^B\d+\.png$/, readdir $dir;
 closedir $dir;

print "@afiles\n";
print "@bfiles\n";

# combine them with minimal error checking
# into a montage
foreach my $afile(@afiles){
  #extract number with regex
  $afile =~ s#^A(\d+)\.png$##; 
  my($num) = ($1);
  print "$num\n";
 
  my @images = ('A'.$num.'.png','B'.$num.'.png');
  foreach my $im(@images){
      my $rc = $image->Read($im);
                 warn $rc if $rc;
    }
  
  print "Montaging $num...\n";
#  my ($montage) = $image->Montage( 
#                  tile  => '1x2',
#                  geometry  => '200x400'
#                  );
  my ($montage) = $image->Montage(); # will do good 
   
   my $out = 'C'.$num.'.png';
   $montage->Write($out);
 
 undef @$image;  #clear out old data for next run
}

exit;
################################################


Good luck,
zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html 



More information about the Magick-users mailing list