Lossless cropping of JPEG images

Normally to crop an image, you'd open it up in an image editor, use the crop function, and then re-save the image.  But with lossy file formats like JPEG, each time you save the file it gets re-compressed and degraded, losing quality like when you copy a cassette tape.  However, the JPEG image format allows for lossless cropping of images.  There are programs that will perform this function for you:

Windows users:

You can use jpegcrop.exe for Windows, which is free.  You'll find it here or here.

Linux users:

You can use the jpegtran program, installed by default on most linux distributions.  To see if you have it, run jpegtran --help and look for the -crop switch.  If you don't have it, or if yours doesn't have the -crop switch, you can get the more recent patched version from this jpegclub.org page under "2. Lossless crop 'n' drop".  At the time of this writing, it's available in a file called croppatch.tar.gz (direct link, local copy) which includes the source code and the binary jpegtran file.

GUI frontend for Linux:

It's a big pain to try to guess the dimensions and offset of the area you want to crop (or to use a program like Gimp just to select a region and note the coordinates), and then type them at the shell prompt.  So I wrote a handy Perl/Tk frontend to jpegtran that displays your image, allows you to select the region you want to crop, and then press the 'c' key to make the crop.  (You use mouse button 3 to make the selection, mouse button 1 to resize it, and the i/o keys to zoom.)  You can get my program here; rename it to losslesscrop.pl and put it somewhere in your PATH.  To use it, you'll need jpegtran and rdjpgcom (both of which are fairly standard tools on linux, and are probably already installed on your system), the Tk and Tk::JPEG perl modules, available here, and of course, Perl itself.

Note: I've been using Perl for quite some time, but I'm new to Perl/Tk.  There are a couple things I've yet to figure out.  In particular, in my script, it's possible to scroll above the top of the photo, and past the left edge of the photo, if the window is larger than the current displayed size of the photo.  I'd like to restrict it so that the photo is always anchored at the northwest corner of the canvas, but can't figure it out.  If you happen to know how to do that, or have any other comments/suggestions, please let me know.