From 5635baa81890804d5b6d2d01eda085449466d1a9 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sun, 30 Oct 2011 00:12:31 +0200 Subject: Detect and use mogrify to auto-rotate JPEG images with EXIF orientation tags. --- fbin.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/fbin.py b/fbin.py index 75a0343..0b8bed4 100755 --- a/fbin.py +++ b/fbin.py @@ -1,7 +1,7 @@ #!/usr/bin/env python2 import templates -import settings, db, os, random, datetime, shutil, mimetypes, cgi, tempfile, hashlib, Cookie, urllib +import settings, db, os, random, datetime, shutil, mimetypes, cgi, tempfile, hashlib, Cookie, urllib, subprocess from PIL import Image base62_alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' @@ -14,6 +14,14 @@ if not os.path.isdir(settings.file_directory): if not os.path.isdir(settings.thumb_directory): os.mkdir(settings.thumb_directory) +try: + # Throws OSError if mogrify doesn't exist. + subprocess.call(['mogrify', '-quiet']) +except OSError: + has_mogrify = False +else: + has_mogrify = True + class Application(object): def get_user(self, username, password): session = db.Session() @@ -222,9 +230,19 @@ class Application(object): hash = f.hash else: hash = self.add_file(temp.name, filename, file_hash, user) + # This avoids silly "not bound to a Session" errors when trying to use a newly added file object. + f = self.get_file(hash) temp.close() + mime = f.get_mime_type() + # TODO: Apparently TIFF also supports EXIF, test this. + if has_mogrify and mime == 'image/jpeg': + # NOTE: PIL doesn't support lossless rotation, so we call mogrify to do this. + # NOTE: This changes the file, so the file_hash applies to the ORIGINAL file contents only. + # NOTE: The file hash is only used to detect duplicates when uploading, so this should not be a problem. + subprocess.call(['mogrify', '-auto-orient', f.get_path()]) + if 'api' in form: start_response('200 OK', [('Content-Type', 'text/plain')]) return ['OK {hash}'.format(hash = hash)] -- cgit v1.2.3