Philip Guo (Phil Guo, Philip J. Guo, Philip Jia Guo, pgbovine)

Desktop Photo Montage

Summary
I describe a simple and fun hack that periodically sets your computer's desktop background to a montage of your own photographs taken at around the same time as the current time.

In late 2011, I came up with a hack idea similar in spirit to Random Browser. I was tired of always staring at the same static desktop background image on my computer all the time. And cycling through shiny stock photographs didn't excite me either. So I thought to myself, "It would be cooler to always be viewing photos from my own personal collection."

Display Heuristics

I decided to write a simple script to periodically create a montage of 8 or so photos from my personal collection and set the desktop background to that montage.

But which photos should my script pick? The most interesting choice to me was to pick a random set of photos taken at around the same time of day and month as the current time. For example, I'm currently writing this article at 8pm on a June evening. Thus, my script creates a JPEG image containing photos taken around 8pm in June during prior years (click for full-size version):

June 8pm
photos

These 8 photos are in chronological order, sampled at random points throughout my personal photo collection. The first two photos are from summer evening LAN parties in high school (June 2001), and the third and fourth photos are of the Southern California sunset in June 2002. The other photos show evening scenes from a summer cruise (June 2004) and also an SF Giants baseball game (June 2007).

Note that this montage contains 8 photos at a total resolution of 1366x768, since that's what fits best on my 11" MacBook Air laptop. My script can be customized to generate a different number of photos or to output a different resolution image.

User experience

Of course, the above photos have no meaning to you, but I immediately recognize and recall memories from looking at this montage. It subtly answers the question, "What was I doing at around this time in the past?"

I set my script to generate a new random montage every two minutes. Thus, as I proceed throughout my work day, my computer's desktop background always "matches" the current time of day—collections of morning photos are displayed in the morning, afternoon photos in the afternoon, evening photos in the evening. And since it always picks photos from the current month, the montage also "matches" the season and time of year. Finally, the randomness provides some amount of serendipity so that I sometimes get pleasantly surprised by what photos pop up.

Of course, I switch off the script whenever my computer is attached to a projector since I have no idea when an embarrassing photo will pop up in the background montage!

Implementing on OS X using Python and AppleScript

Before starting this project, I wrote some Python scripts to import metadata about all of the digital photos in my personal collection into an SQLite database. My scripts extracted photo metadata such as time/date info from the EXIF headers in each JPEG file. You can write whatever code you want to do this pre-processing step; my code probably won't be useful for you, since it's hard-coded for my setup. The only metadata you need about each photograph is its filename and time/date info.

I then wrote a Python script called create_background_montage.py that queries the OS for the current time/date, queries my SQLite database for photos taken around this time and month in the past, randomly samples from this set, and then uses PIL to create a photo montage as an output JPEG image.

One useful heuristic is for my script to pick at most one photo from a particular sub-directory (which is a proxy for an "event") so as to avoid displaying more than one photo from any individual event in the montage. However, that's not always possible, so this restriction gets lifted if there aren't enough available photos that fit the primary time-of-day/month criteria.

To get this script to work on OS X, I added yet another gross hack: I made it alternate the filename of the output JPEG montage between background-A.jpg and background-B.jpg (you'll see why this is necessary in the next section on AppleScript). Thus, after each invocation of the script, it generates the appropriately-named image and then prints (to stdout) the full absolute path of the image in Mac-style paths with a ':' character as the directory delimiter. Thus, the first run of create_background_montage.py generates background-A.jpg and prints:

:Users:pgbovine:Desktop:background-A.jpg

(This indicates that it just generated /Users/pgbovine/Desktop/background-A.jpg.)

Then the next run deletes background-A.jpg, generates background-B.jpg and prints:

:Users:pgbovine:Desktop:background-B.jpg

... and so on. Ok, why all these contortions? Read on ...

The final piece of the system is an AppleScript script that periodically calls create_background_montage.py and changes the Mac OS X desktop background image to the auto-generated montage image. Here it is:

on idle
  set s to do shell script "/Library/Frameworks/Python.framework/Versions/Current/bin/python /Users/pgbovine/Desktop/create_background_montage.py"
  delay 5 -- wait for a bit so that the new background image has time to render
  tell application "Finder"
    set desktop picture to {s} as alias
  end tell
  return 120 -- timeout
end idle

This script is ugly due to the need to write absolute paths. The long line starting with set s invokes create_background_montage.py and sets its printed output to a variable s. Now you see why I had to print out the output montage file's absolute path (with Mac-style colon directory delimiters!) to stdout. Next there's a delay 5 line to give the Python script enough time to finish running. Then it tells the "Finder" application to set the desktop background to the image that s refers to. Finally return 120 makes the script sleep for two minutes (120 seconds) and then repeat the loop.

Ok, so why all of this fuss over alternating between background-A.jpg and background-B.jpg? Because it turns out that if the AppleScript script just kept on telling the "Finder" application to set the background image to the same filename, then it won't actually do anything. It probably assumes that the image was unchanged, but of course, a new montage has been generated, so the image is different! The way to circumvent this annoyance is to keep generating montages with different filenames; it turns out that two is sufficient.

Ok, that's all for now, folks!

Created: 2011-10-18
Last modified: 2013-06-15
Related pages tagged as software: