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

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\n

Automatically 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\n";

Postscript from JPEG

If you have had the experience of receiving big postscript files when you have converted a file from JPEG format to postscript these comments might help also you. Postscript can describe the bitmapped files directly (which gives large files) but it can also embed jpeg-files which maintains the compression. The utility jpeg2ps does the trick (jpeg2ps is usually bundled in the standard latex distibutions e.g. fpTeX). To convert a file just run
jpeg2ps -r 300 image.jpg
and you will receive an encapsulated postscript file image.ps which has the desirable small size (in this particular case the resolution is 300 dpi). See the options for the command jpeg2ps for more options.

Postscript manipulation

Here is how I have rotated a ps-file upside down using the pstops utility that is included in the psutils package. The package psutils is usually included in the standard LaTeX distributions.
pstops "1:0U(21cm,29.7cm)" oh.ps oh_new.ps