[Kc] Meeting Minutes: 2003_02_11
Frank Wiles
frank at wiles.org
Wed Feb 12 11:20:13 CST 2003
.------[ Garrett Goebel wrote (2003/02/12 at 10:46:09) ]------
|
| Michael Morgan mentioned an interesting problem which I urged him to present
| on the list. But now that I've brought it up, I'll go ahead and beat him to
| it. The question was how to write a script which will read in a list of all
| image files in a particular directory, rename them using an ordered sequence
| of 1.jpg .. 10.jpg, and resize each image to 640x480. It sounds like a
| problem many of us have faced at one time or another. If anyone would like
| to post partial or complete solutions to the list, I and I'm sure Michael
| would appreciate it.
|
`-------------------------------------------------
This script assumes a Unix system, the existence of Image Magick
in the path, and working only with JPG files. Modifying it for
other setups shouldn't be hard.
Also, I took into account the case of wanting to move images that
already had <number>.jpg names by renaming the original to
<number>.jpg.orig and the making the resized version normally as
<number>jpg.
The Image Magick options are pretty self explanatory, but here they
are:
-size 640x480 gives it a hint that the final product will be this
size, which supposedly speeds it up
<original filename>
-resize 640x480 tell it to resize to the given size
<new filename>
My script should be used like so:
script.pl /path/to/convert/
script.pl --directory=/path/to/convert
script.pl --start=415 /path/to/convert
script.pl --start=415 --directory=/path/to/convert
You can call it with just a directory to start sequencing at 1 or
set the sequence start number with the --start option. --directory
is just there because it was easy to do. :)
Hope this helps let me know if you have any questions about it.
Script follows, tested as well as I could on my system.
---------------------------------
Frank Wiles <frank at wiles.org>
http://frank.wiles.org
---------------------------------
#!/usr/bin/perl
use strict;
use Getopt::Long;
#####################################
# Variables
#####################################
my $directory; # Directory to work on
my $start_seq = 1; # Number to start sequence at ( defaults to 1 )
my @files; # Array to hold original file names
#####################################
# Begin Main Execution
#####################################
# Parse commandline options
GetOptions(
'start=i' => \$start_seq,
'directory=s' => \$directory
);
# Handle case of script.pl <directory>
if( scalar(@ARGV) == 1 ) {
$directory = $ARGV[0];
}
opendir( DIR, $directory ) or die "Cannot open directory '$directory': $!\n";
# Loop on all the files in here
while( my $f = readdir(DIR) ) {
next if $f =~ /^\./; # Skip hidden files
next if $f !~ /\.jpg$/; # Skip non JPEG files
push(@files, $f);
}
closedir(DIR);
# Process each file
my $seq = $start_seq;
foreach my $f ( @files ) {
# Handle already existing <number>.jpg
if( -e "$directory/$seq.jpg" ) {
rename("$directory/$seq.jpg", "$directory/$seq.jpg.orig");
$f = "$seq.jpg.orig";
}
# Actually convert the images using Image Magick
`convert -size 640x480 $directory/$f -resize 640x480 $directory/$seq.jpg`;
# Increment the sequence
$seq++;
}
More information about the kc
mailing list