jhead: Underated Image Manipulation Tool

October 9, 2004
By Christopher

After messing around and getting nowhere working on the SuziTech web site this afternoon I decided to go do some violence to our on-line family album.

I’ve got approximately 1150 images that I need to re-input into gallery. I had been lazy the last 9 months or so and as a result all my images that had not been categorized had lost their correct file system time stamp. Moved from temp folder to temp folder, pulled off CD media (old stuff), etc., etc.

I was going to rely on using iPhoto to categorize the files by date. I had noticed that iPhoto imports stuff using a date structure so it should be just a matter of doing the import and then use the galleryadd.pl script to import the directories into the family album web site.

As it turns out – iPhoto uses the file system modification date. Not the file creation date or the capture date that is in the JPEG EXIF header embedded in the file itself. Kinda dumb. iPhoto should know better.

Now what? After googling a bit and getting no where (java apps? Give me a break!). I really wasn’t interested in writing a PHP script to interface to the EXIF libs. I started thinking about the UNIXey tools that the gallery system runs on. Gallery has the EXIF info so it must extract somehow so I started googling on that.

Hmm… jhead does some stuff… Ahh ha! jhead is capable of spraying the EXIF header to stdout!

So, to view the EXIF info on the cmd line it’s just "jhead filename". An example:

chris@monolith<535>~/Desktop/BULK_MOVE $ jhead DCP_1701.jpg
File name    : DCP_1701.jpg
File size    : 603528 bytes
File date    : 2004:03:31 19:13:44
Camera make  : EASTMAN KODAK COMPANY
Camera model : KODAK DC4800 ZOOM DIGITAL CAMERA
Date/Time    : 2004:03:31 19:13:44
Resolution   : 2160 x 1440
Flash used   : Yes
Focal length :  5.9mm
Exposure time: 0.125 s  (1/8)
Aperture     : f/2.8
Focus dist.  : 0.63m
Exposure     : program (auto)
Jpeg process : Baseline

A little bit of sed/awk and this should be easy to pull off.

I’m glad I read the man page at this point. The “-ft” switch does exactly what I want: pull the Date/Time field from the EXIF header and apply it as the last modified file system date.

Quick test, apply it to all the files: "for i in $(ls -1); do jhead -ft $i; done" and voila, Bob’s your uncle – or so "ls -lt" reveals.

Ok, fire up iPhoto again and import after wiping the previous content…and…Yes! Success. Example:

chris@monolith<586>~/Pictures/iPhoto Library $ tree -L 3 200*
2001
|-- 11
|   |-- 18
|   |   |-- DCP_0268.jpg
|   |   |-- DCP_0269.jpg
|   |   |-- DCP_0271.jpg
|   |   |-- Data
|   |   `-- Thumbs
|   |-- 26
|   |   |-- DCP_0276.jpg
|   |   |-- DCP_0277.jpg
|   |   |-- DCP_0278.jpg
|   |   |-- Data
|   |   `-- Thumbs
|   |-- 28
|   |   |-- DCP_0279.jpg
|   |   |-- Data
|   |   `-- Thumbs
|   `-- 30
|       |-- DCP_0280.jpg
|       |-- DCP_0281.jpg

[...snip...]

Now, if you don’t have access to an application like iPhoto you could use bash/sed/awk to do the file sorting into directories. In fact this may be preferred given what I found out about iPhoto next.

Because the import in iPhoto is copying the source file to a new destination the file’s time stamp is set to current time again. Crap! Not a big deal but an extra step that a proper sorting script would avoid using the correct "cp" command line switches. Ok, to fix the file time stamp again:

chris@monolith<589>~/Pictures/iPhoto Library $ for i in $(tree -aif | grep jpg | grep -vi thumbs); do jhead -ft $i; done

and let it rip.

Now, you can use the standard galleryadd.pl script available at the Gallery home page [ http://gallery.menalto.com ] to import this hierarchy of directories into gallery from the command line. An example:

chris@monolith<657>~/Desktop/galleryadd/2002 $ /home/apache/bin/galleryadd.pl -l http://monolith.moose.ca/modules/gallery/ -a FaF_2002 -u admin -p passwd * -c
Logging In
Logged In successfully
Fetching list of albums (may be slow)
Fetched list of albums.
Found album 'FaF_2002'
Creating Album '01'
Album '01' created successfully
Creating Album '01/04'
Album '04' created successfully
Uploading image '01/04/DCP_0308.jpg'
Image '01/04/DCP_0308.jpg' uploaded successfully
Creating Album '03'
Album '03' created successfully
Creating Album '03/08'
Album '08' created successfully
Uploading image '03/08/DCP_0315.jpg'
[...snip...]

You’ll notice I am working out of a copy of the iPhoto directory with all the Data and Thumbs dirs/files removed. galleryadd.pl has issues with these files.

Hope someone finds this useful. FWIW – I’m nearing about 5 years worth of photos in the on-line gallery. The only way to keep it navigatable (is that a word?) is to break it down by dates. At this point it is starting to get kinda crufty at that level and I am starting to think that postnuke news articles for special events in the album are starting to make sense.

Btw, neither jhead or tree come with most distros by default. You’ll need to access portage/yum/fink/whatever in order to get these little programs. I use tree because I find it more intuitive than “find blah -exec blah”.

Leave a Reply