def uploadFile(filename): exif = photoutils.getExifData(filename) i = Image.open(filename) doc = {} doc['width'], doc['height'] = i.size doc['keywords'] = ['unprocessed'] doc['descr'] = 'Uploaded image.' doc['size'] = os.stat(filename).st_size doc['extension'] = FORMATS.get(i.format, i.format).lower() doc['tnwidth'], doc['tnheight'] = photoutils.scaleDims(i.size, TNSIZE) doc['cat'] = 'Private' doc['addedby'] = getpass.getuser() doc['type'] = 'photo' doc['ts'] = time.strftime("%Y-%m-%dT%H:%M:%S") doc['taken'] = takenDate(exif) doc['_id'] = md5File(filename) doc['exif'] = exif ext = doc['extension'] mimeType = 'image/' + i.format.lower() # Save it to S3 before it hits the DB. Mildly annoying when # there's a failure, but I'd rather not think I've got photos I've # got than vice versa. saveS3(doc['_id'], filename, ext, mimeType) docid, rev = db.save(doc) saveScaled(docid, i, (800, 600), '800x600.' + ext, mimeType) saveScaled(docid, i, TNSIZE, 'thumb.' + ext, mimeType)
def saveScaled(docid, i, size, name, contentType): scaled = i.resize(photoutils.scaleDims(i.size, size), Image.ANTIALIAS) fn = "/tmp/uploadtmp.%d.%s" % (os.getpid(), name) scaled.save(fn) try: saveAttachment(docid, fn, name, contentType) finally: os.unlink(fn)