December 12, 2005

Converting BINS metadata to iPhoto

Before I used a Mac and photo management software like iPhoto, I simply had all of my images in hierarchy layout of directories in my /home/crafterm/images path, and used a great package like BINS to render my images to a website that I could upload to a server.

Bins-Site

BINS did a really good job and had some nice theming abilities, but since the rendering was done on my workstation, the site that was created was static and didn't have any interactive features like comments, view counts, favourites, etc.

One of the things I liked most about BINS was that it used XML to store all of it's meta data about each image (including extracted EXIF data as a backup), and there was a graphical tool for editing everything. I always thought, if I ever needed to get the data out of BINS, at least XML was going to let me do it.

Fast forward a few years, and this is exactly what happened. I ended up getting a Mac as my primary work machine, and this whole world of new multimedia applications opened up to me, particularly the free and bundled iLife suite of iPhoto, iTunes, iMovie, iDVD, Garageband, etc.

Also, Flickr captured a lot of the online photo publishing community with it's social, and interactive nature. So, like many others, I ended up getting a Flickr pro account, and started uploading using the Flickr export plugin from Connected Flow

The problem was essentially that after importing all of my images into iPhoto, all of the descriptions that I'd entered into BINS were missing. The data was there in the BINS XML files alongside each image, but as expected it wasn't in iPhoto.

So, I started prodding around with AppleScript and found out via the iPhoto AppleScript dictionary that it's possible to select an image inside the iPhoto library by filename, and assign a comment (amongst other things) to it.

I whacked together a script to do this:

-- Sets the comment of a particular image in iPhoto
-- Expects image title as first argument, and comment as second

on run argv
	set myTitle to item 1 of argv
	set myComment to item 2 of argv
	
	tell application "iPhoto"
		set comment of photos whose image filename is myTitle to myComment as text
	end tell
end run

This script must be invoked from the command line and it accepts two arguments, the first being the filename of the image, and the second being the comment you wish to set that image to.

You can invoke the script as follows:

$> osascript iPhotoCommentScript IMG_1234.JPG "My new fancy comment"

With this script, all that was needed now was the ability to iterate over the BINS XML files, extracting the image filename, and attributes such as the description, location (and perhaps other things later like people, and the other custom tags BINS let's you specify).

To do this I wrote a simple Ruby script:

#!/usr/bin/env ruby
#
# Given a directory of BINS image description files, read the description and location of
# each image, and pass this data on to iPhoto as the comment for that image.
#
# Currently reads all xml files in the current directory, skipping album.xml.

require "rexml/document"

files = Dir["*.xml"]

files.each { |file|
    next if file == "album.xml" # skip this since it's not an image
    
    xmlDoc = REXML::Document.new File.new(file)
    location = xmlDoc.root.elements["/image/description/field[@name='location']"]
    description = xmlDoc.root.elements["/image/description/field[@name='description']"]
    imagename = file.sub(/\.xml$/, "")

    location = location == nil ? "" : location.text.strip
    description = description == nil ? "" : description.text.strip

    comment = description

    if (location != "") 
      if (description == "")
        comment = location
      else
        comment += ", " + location
      end
    end
    
    system "osascript ~/Desktop/iPhotoSetComment.scpt #{imagename} \"#{comment}\""    
    puts "Updated description/location for image #{imagename}"
}

And that's it. All I needed to do was simply run the Ruby script in each directory of my BINS photo library hierarchy, to update each photo's corresponding comment in my iPhoto library.

Since BINS allows you to set extra meta data fields such as image location, (and others such as the names of people who were there, etc) the Ruby script combines the description and location into a single string. It's best to review the resultant string for grammar, etc if you'd ended your description with an exclamation mark or something. You might want to extend the Ruby script to extract the other fields and combine it into a more complete description string.

Also, even if you don't use BINS, the AppleScript script itself might be useful for merging data from other photo library formats.

Next step is to take the comment fields from iPhoto and write them into the EXIF tag of the files themselves so that they stay with the images where-ever they go. Once I find a Ruby library that lets you write EXIF meta-data I'll take a look into this as well.

Technorati Tags: , , , , ,

Posted by crafterm at December 12, 2005 02:24 PM | TrackBack
Comments
Post a comment









Remember personal info?