Notes on picture and figure manipulation
This page contains some notes that I have made about image manipulation, and
since I want the information to be available for me all the time I am
putting it on the Internet. Maybe it will be useful for someone else
sometime. The page is mainly about manipulating images with free tools on
both Unix and Windows platforms. The material that I have put together is
available on manual pages but since I am lacy I have collected the most
frequently used commands and options here. First some history:
It all started when I wanted to make a homepage from Postdoc in Switzerland
and very conveniently got (requested) a CANON Digital IXUS from my sister in
a combined christmas gift, 30 years present, christmas gift, and 31 years
present. The camera produces pictures with 1600x1200 pixels and when
publishing them on a webpage it is nice to be able to scale them and/or to
make small thumbnails for previewing the images.
The tools
- Cygwin for windows.
- find - A very potent unix function that recursively searches files in the file tree.
- JPEG library.
- jpegtrans - Rotates and performs lossless transformations of jpeg images.
- djpeg - Uncompresses and transforms jpeg images to different formats.
- cjpeg - Compress different pictures to jpeg format.
- Canon PhotoStitch, a program for stitching images together to generate
panorama pictures. It is unfortunately a commercial product but it is very
simple to use and followed with my camera.
- imagemagic - I have been told that this set of tools is very good for
converting images but I have not yet had the time study it.
Changing size of a JPEG-image
The function djpeg
converts images jpeg-format to other formats
such as bmp, gif (without compression), os2 (bmp), ppm/pgm (default
pgm=greyscale), and targa. Here follows some examples.
Transform a color file to a small preview
djpeg -scale 1/8 -pnm -outfile outFile.ppm test.jpg
djpeg -scale 1/8 -outfile outFile.ppm test.jpg
Fast decompression for prewiev,
djpeg -scale 1/8 -bmp -greyscale -outfile preview.bmp test.jpg
The function cjpeg
makes the inverse transformation and is very
convenient, to use. Here is an example
Make a new jpeg image for preview
djpeg -scale 1/8 test.jpg | cjpeg -quality 50 -optimize -outfile test_sm.jpg
Generating thumbnails
I have adopted the following standard for my thumbnails and this is what has
been guiding my development of perl functions for generating previews and
thumbnails: If I have an image file with with name
big_Image.jpg
the small image that is the preview will have a
suffix _sm
added before its extension i.e. it will be named
big_Image_sm.jpg
.
My Batch Conversion of thumbnails
My perl-function for generating a bash script that will generate some
previews. The bash script is named makePreview.pl
and it takes
a filename or reads from the standard input and generates a shell script,
that when it is executed it generates a set preview pictures for all the
pictures in the library.
#!/usr/bin/perl -w
print "#!/usr/bin/bash\n";
while (<>) {
chomp;
$currentFileName=$_;
$newFileName=$currentFileName;
$newFileName=~s/(.jpg)$/_sm.jpg/i;
$commandLine="djpeg -scale 1/8 $currentFileName | cjpeg -optimize -quality 50 -outfile $newFileName\n";
print $commandLine;
}
print "#\n";
I call the function and the genereated shell script and with the following
sequence of commands.
$ find Italien/ -iname '*.jpg' | ./makePreview.pl > renameItalien.sh
$ ./renameItalien.sh
My generation of a picture preview page
This perl function takes a list of jpeg-files from standard input and
generates an html-file on standard output that gives the
pressure. Unfortunately there is some error in my HTML code here so that the
perl code gets formatting, but this formatting is commented.
#!/usr/bin/perl -w
\n\nAutomatically generated preview file
\n";>
while (<>) { # reads a line that contains a filename
chomp; # Remove newline from the end of the line
if (/.jpg$/i){ # Check that it is a jpeg file
unless (/_sm.jpg$/i){ # check that it is not already a preview file
if (-e $_){
s/^\.\///;
$viewFileName=$_;
# Add the postfix to the filename
$viewFileName=~s/(.jpg)$/_sm.jpg/i;
if (-e $viewFileName){
# Generate the preview line
$theLine="\n";
} else {
$theLine="$_\n";
}
# Print the preview line.
print $theLine;
}else{
die "File $_ does not exist";
}
}
}
}
print "\n