EXIF to Thumbnail Managing Standard converter

Ultra-fast EXIF thumbnails for gqview/gimp/gnome/thunar/etc.

Motivation

After I recently purchased a Canon digital camera, I found out that my old laptop can't really handle the 8-Mpix JPEG files weighting circa 4.5MB each. What could you expect from P3/750? Displaying the files in gqview is a bit slow, although enabling precaching of next image and setting resampler quality to minimum speeds things up a nice bit. However, thumbnail generation is a complete pain. I have no idea what algorithm they used to generate thumbnails, but looks like it loads the entire JPEG just to downscale it to 128x128px (and the downscaler is of really bad quality).

As I've heard of image viewers supporting EXIF thumbs, I started to wonder if they could be implemented in gqview too. I thought of modifying the C source, but it's not only ugly, but requires recompiling, testing and debugging code someone else wrote (and guess what - gqview's code isn't particularly beautiful). After all, I'm a Perl man, so... yes, yes, a kludge, a kludge!

Operation

Okay, my kludge is actually pretty nice. It doesn't actually do anything to gqview, everything is happening outside it.

As we all know ;), gqview, among others (gimp, gnome, thunar, etc.) uses the Thumbnail Managing Standard. Its scheme of operation involves creating an URI of the file, hashing it with md5, and storing the thumbnail (in PNG format) with the hash as a filename. Pretty simple, isn't it? So, that's exactly how my script works:

  1. Grab a JPEG file path, generate the URI and its MD5 hash.
  2. Use exif command line tool to extract the thumbnail.
  3. Again, use exif utility to extract the file rotation.
  4. Use ImageMagick to convert the JPG thumbnail to PNG, scale and rotate it in the process.
  5. Add tEXt fields to the PNG file with proper metadata (OBEY THE SPECS!)
  6. Voila - thumbnail generated!

The worst part of the specs was adding tEXt chunks to the PNG file. Apparently, there is no command line utility to do it! Or not? Eric Raymond's excellent sng is a great example of how UNIX metodology helps us not to duplicate the same functionality over and over. Simply put, it's a PNG compiler/decompiler. With its help, adding new chunks to PNG file is as simple as decompile - append a few lines of text - compile. All this without ever having to learn about the structure of PNG files or the libpng API.

Performance

YMMV, but here are my results from IBM T21 laptop with P3/750MHz and 8Mpix ~4MB JPEGs.

That's exactly 6x improvement in speed. This could be further improved if the functionality was built into gqview, as it has access to all EXIF data and doesn't need to extract it over and over again.

There is one more difference between gqview- and EXIF-generated thumbnails - my script automatically rotates the EXIF ones so if you enable 'auto rotate using EXIF information' in gqview, you get consitent picture+thumb. The rotated thumbs are also visible in e.g. Thunar when using its thumbnail preview functionality (actually, Thunar's thumbnail generation engine is superior in speed to that of gqview!).

Thumbnail comparision

gqview EXIF

As it can be seen above, EXIF thumbs are generated using simple 'nearest' algorithm and no blur is applied to them. As whether the over-blurred gqview version or the over-sharpened EXIF one is preferred, it's the user's choice. Remember that you can always feed the convert command with -blur 2x2 and get similar results in much faster time.

TODO

Usage

Simply run the script against some JPEG files you have and it will generate the thumbs and store them in ~/.thumbnails. Afterwards, you can use any TMS-supporting app to view the thumbs.

exif2tms <JPEG files> ...

The scripts requirements are:

Download