Python davclient+PIL => my gallery uploader

Vista Vista Vista. Oh how you hate me.

It turns out the XP “Publish and Print” wizard – or whatever it was called – integrated great with Gallery just by adding a simple registry key and enabling the right Gallery add-ins. Vista doesn’t have this. Vista has instead a lame f-spot rip-off (ok, it is probably an iPhoto rip-off) called Windows Photo Gallery. It isn’t bad. It isn’t great. The XP publish wizard allowed me to resize images before sending them to an internet printer or Gallery. WPG doesn’t allow this. It turns out I can use the same Gallery reg and change just the registry path and it will work with WPG. But Gallery has a file size limit of two megabytes. I shoot my Canon S3 IS on its “SL” image setting so the file size is usually three or four megabytes.

Print and Publish and WPG doesn’t work for my case in Vista, so I’ll try WebDAV. I should just be able to “add a new network place” like in XP and type in the Gallery URL and then just drag and drop files (once they are resized of course). This is also a no go. It turns out that Microsoft is WAY smarter than me. Vista has disabled the ability to use WebDAV over HTTP. It can ONLY be used over HTTPS. Never mind that I don’t care about my plain text password in this case. It doesn’t matter. Vista is smarter than me.

Unwilling to manually resize and upload even a handful of files, I decided to script my solution. With only six hours until the MichiPug meeting I thought it would be fitting to use Python as my tool of choice. The bonus to using Python is that the solution will also run in Linux and since all of my Photos are stored on a Linux server running Samba, I’ll be able to publish photos remotely via ssh from my server if I want to (and if I happen to know the filename I want to publish).

It turns out resizing images easy trivial in Python thanks to PIL(Python Image Library).

import Image
width=1600
height=1200
imageFile="path/to/IMG_0100.JPG" im1 = Image.open(imageFile)
im5 = im1.resize((width, height), Image.ANTIALIAS)

tempdir = tempfile.gettempdir()
filename = os.path.basename(imageFile)
tempfilelocation = os.path.join(tempdir,filename)
im5.save(tempfilelocation)

So resizing the image was easy. Getting a filename from argv[1] was easy. I wasted a few hours barking up the wrong tree with davclient. Ultimately it was my own fault for not opening the file with ‘rb’. On Windows that ‘b’ matters! I’m used to python development on Linux where it is a meaningless.

At the end of the day I have a cmd script in my Send To folder that calls this python script

#!/usr/bin/python

import Image
from sys import argv
import tempfile
import os
import davclient

#site must include trailing slash
site = 'http://www.wrenfam.com/gallery/w/SomeGallery/'
username='myusername'
password='mypassword'
width=1600
height=1200

def imageTempResize(imageFile,width,height):
        im1 = Image.open(imageFile)

        im5 = im1.resize((width, height), Image.ANTIALIAS) # best down-sizing fi
lter
        tempdir = tempfile.gettempdir()
        filename = os.path.basename(imageFile)
        tempfilelocation = os.path.join(tempdir,filename)
        im5.save(tempfilelocation)

        return tempfilelocation

def davUpload(tempName, site, username, password):
        client = davclient.DAVClient(site)
        client.set_basic_auth(username,password)
        fname = os.path.basename(tempName)
        file = open(tempName,'rb')
        statinfo = os.stat(tempName)
        fsize=statinfo[6]
        url = site+fname
        contents = file.read(fsize)
        if len(contents)!= fsize:
                return

        client.headers['Content-Type'] ='application/octet-stream'
        client.headers['Content-Length'] = len(contents)
        client.put(url,contents)
        status = client.response.status
        if status==204 or status==201:
                print "file %s uploaded successfully" % fname
        else:
                print "something went bad when sending :( "
                print "client.put(%s,f=%s)" % (url, file)
                print status
                print client.response.getheaders()
                print client.response.body

for i in range(1,len(argv)):
        tempName = imageTempResize(argv[i], width, height)
        davUpload(tempName, site, username, password)

Now I can upload images just by clicking. Of course two big todos are to copy exif data. PIL has no support for writing exif data. pexif is in the cheese shop. It will get done. The second big todo is to support choosing to which to upload.

crying Lily

2 thoughts on “Python davclient+PIL => my gallery uploader”

  1. The 2MB limit is not a Gallery limit, thats the default PHP upload size limit. Gallery has to obey the php.ini setting for uploads, if you host could increase that you could upload larger images without resizing them at all.

    There are also several existing script based Gallery uploaders around, in various languages ( http://codex.gallery2.org/Other_Clients ).

    Nice job on the python though. 🙂

Comments are closed.