Joseph Jude

Accessing Flickr Photos with Python

2008.06.29 / code / django /

Working python code to access flickr photos from python

Flickr lists few python libraries to access its API. Of these, I choose Beej's Python Flickr API. Probably the reason being it had extensive documentation (and it appeared first!).

Choosing the library was only one part. I had to spend most of the time (almost the whole of today) reading through the Flickr API. Once you understand the API, it becomes easy.

My requirement for S-I-P is pretty simple. Given a photo id, I need photo details (title, description), EXIF details and size details (thumbnail, original). To access Flickr API, you need to apply for a API Key.

Here is the simple program to access Flickr Info given a photo id. Most of it should be self explanatory. For more details, read through Flickr API documentation.

I'm still, relatively, a newbie in Python and Django. So if you think there could be a better way to do this, feel free to leave a comment.

def get_photo_info(api_key, photo_id): f = flickrapi.FlickrAPI(api_key)

info=f.photos_getInfo(photo_id=photo_id)
title = info.photo[0].title[0].text
desc = info.photo[0].description[0].text
#url = info.photo[0].urls[0].url[0].text
tags = ' '.join(["%s" % (tag.text) for tag in info.photo[0].tags[0].tag])

exif = f.photos_getExif(photo_id=photo_id)
model = exif.photo[0].exif[1].raw[0].text
date_taken = exif.photo[0].exif[2].raw[0].text
exposure = exif.photo[0].exif[4].raw[0].text
aperture = exif.photo[0].exif[5].clean[0].text
exposure_prg = exif.photo[0].exif[6].clean[0].text
iso = exif.photo[0].exif[7].raw[0].text
exif_details = dict(model=model, date_taken=date_taken, exposure=exposure, aperture=aperture, exposure_prg = exposure_prg, iso=iso)

#getSize gets both size (thumbnails etc) and corresponding urls
sizes = f.photos_getSizes(photo_id=photo_id, format='json')
#there should be a better way to do the below
#flickr returns jsonflickrapi(....) as string
#i am retriving the values
sizes = simplejson.loads(sizes[14:-1])['sizes']['size']
for size in sizes:
    if size.has_key('label'):
        if size['label'] == 'Square':
            thumb_url    = size['url']
            thumb_height = size['height']
            thumb_width  = size['width']
            thumb_source = size['source']
        if size['label'] == 'Large':
            photo_url    = size['url']
            photo_height = size['height']
            photo_width  = size['width']
            photo_source = size['source']

thumb_details = dict(url=thumb_url, height=thumb_height, width = thumb_width, source=thumb_source)
photo_details = dict(url=photo_url, height = photo_height, width = photo_width, source=photo_source)

return dict(title=title, desc=desc, tags = tags, exif=exif_details, thumb=thumb_details, photo=photo_details)

Related Links:

Flickr API

API for getting Photo Information

API for getting Photo EXIF

API for getting Photo Sizes

Flickr JSON response


Share this post on

Twitter | | |
Sign up for my weekly newsletter

I will send blog updates to this email. You can unsubscribe at any time using the link in those emails.