Uploading photos to Shutterfly via Python

I finally got done sorting my 2008 photos this morning so I can start putting Christmas gifts together with them and needed to upload about 280 pictures to Shutterfly, where I have been getting prints since about 1999. Of course I could do this with their uploader, but that would be quite lame now, wouldn’t it, considering I could use Python to do it. Using the code below, the IPTCInfo module and Jeremy Slater’s very cool shutterfly module he wrote for the GNOME Conduit Project , I can iterate through my entire 2008 photo directory, look for my photos tagged for upload, and upload them to a specific album on Shutterfly. Sweet!

  1. from IPTC import IPTCInfo
  2. import sys, os, shutterfly
  3.  
  4. class ShutterflyUploadr:
  5.  
  6. def __init__(self):
  7. pass
  8.  
  9. def grab_new_images( self ):
  10. """ Recurses thru directories and looks for images to upload. I only want to upload my '5-star' images, so we scan the IPTC tags for 'r5', my way of tagging my pics I really like (usually get prints of r5s) """
  11. images = []
  12. for dirpath, dirnames, filenames in os.walk('/users/chad/pictures/2008'):
  13. for f in filenames :
  14. # Grab IPTC keywords
  15. info = IPTCInfo(os.path.join(dirpath, f))
  16. # Is it a 5-star photo?
  17. if 'r5' in info.keywords:
  18. ext = f.lower().split(".")[-1]
  19. if ( ext == "jpg" ):
  20. images.append( os.path.normpath( dirpath + "/" + f ) )
  21. images.sort()
  22. return images
  23.  
  24. def upload( self ):
  25. """ Upload images to Shutterfly """
  26. user = 'your_user_name'
  27. pwd = 'your_password'
  28. sfly = shutterfly.Shutterfly(user,pwd)
  29. # Get an existing album
  30. album = sfly.getAlbums()['Album_Name']
  31. image_list = self.grab_new_images()
  32. for image in image_list:
  33. album.uploadPhoto(image, 'image/jpeg', os.path.basename(image))
  34.  
  35. if __name__ == '__main__':
  36.  
  37. sfu = ShutterflyUploadr()
  38. sfu.upload()

3 Comments

  1. Riz says:

    Can you make a PHP version of this code? That will be really helpful.

  2. chad says:

    @Riz: Sorry, I don’t do that much PHP. And besides, Python is so much more elegant and efficient.

  3. iJames says:

    Hey there,

    Have there been any changes to this code? I’ve been wracking my brains trying to get it to upload. See here: http://stackoverflow.com/questions/2785496/a-python-wrapper-for-shutterfly-uploading-an-image

    And shuttefly tells me there was no image on the upload.

    Any insights into how to use python to get it to correctly upload would save my life.

    Thanks!

Leave a Reply