diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2011-10-30 00:12:31 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2011-10-30 00:12:31 +0200 |
commit | 5635baa81890804d5b6d2d01eda085449466d1a9 (patch) | |
tree | ffec47e8bec4f527451fce4ffa7cb3d1637be7c5 | |
parent | 121798bae3b94492826706017c1a2d0f0d71454a (diff) |
Detect and use mogrify to auto-rotate JPEG images with EXIF orientation tags.
-rwxr-xr-x | fbin.py | 20 |
1 files changed, 19 insertions, 1 deletions
@@ -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)] |