Monday, October 28, 2013

Downloading Picasa Albums with Python...

Well, I'm ready to move my photos out of Picasa onto a new website (more on that later), so I wanted to download all my albums in their original resolution so I can edit and upload to the location. Well, after digging around out there I found that the Google api's will make this easy and there were even some Python bindings to make it easier. However, the api's are not exactly well documented (at least from what I could tell). I searched around for some open source or examples and there are a few out there, but the ones I tried either didn't work or they only downloaded low-resolution images. So it was time to try the DIY technique.

Starting with some of the sample code that comes with the api and using a lot of inspection of object methods and types I was able to put together a script that downloads all the albums. One of the trickiest parts was figuring out how to get the original resolution. At first all attempts seemed to always download 512 resolution samples. It turns out there's a magic option that you need to insert on one of the url's to get it to switch to the full-res download: "imgmax=d". The api doesn't seem to give any clue about this.

Anyway, without having to learn too much about the nitty gritty details of the api I was able to get what I needed. So, in case this helps someone else here it is:
------------------------------------------------------------------------------
import sys, os
import gdata.photos, gdata.photos.service

pws = gdata.photos.service.PhotosService()
pws.ClientLogin('yourid@gmail.com', 'your password')

#Get all albums
albums = pws.GetUserFeed().entry
print dir(pws)

for album in albums:
    print "ALBUM: %30s [%3d]" % (album.name.text, album.numphotos)
    uri = album.GetPhotosUri()
    uri += "&imgmax=d"

    photos = pws.GetFeed(uri).entry
    aname = album.name.text
    aname = aname.replace(" ", "_");

    for photo in photos:
        pname = photo.title.text
        print "PHOTO:", photo.title.text, int(photo.width), int(photo.height)

        url = photo.GetMediaURL()
        media = pws.GetMedia(url)

        # print media.file_name, media.content_type, media.content_length
        data = media.file_handle.read()
        media.file_handle.close()

        filename = "%s/%s" % (aname, pname)
        print "Output: %s" % filename
        sys.stdout.flush()
       
        if not os.path.isdir(aname):
            os.mkdir(aname)

        out = open(filename, 'wb')
        out.write(data)
        out.close()

No comments: