Fixed EXIF longitude bug
[mediagoblin.git] / mediagoblin / tools / exif.py
index 445907ba80efd5a7057c70ddf20b7066a92ddf33..c4fc1fe5a5b5bce0422df68da0471c5644f79432 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
@@ -18,8 +18,6 @@ from mediagoblin.tools.extlib.EXIF import process_file, Ratio
 from mediagoblin.processing import BadMediaFail
 from mediagoblin.tools.translate import pass_to_ugettext as _
 
-from collections import OrderedDict
-
 # A list of tags that should be stored for faster access
 USEFUL_TAGS = [
     'Image Make',
@@ -34,6 +32,15 @@ USEFUL_TAGS = [
     'EXIF UserComment',
     ]
 
+
+def exif_image_needs_rotation(exif_tags):
+    """
+    Returns True if EXIF orientation requires rotation
+    """
+    return 'Image Orientation' in exif_tags \
+        and exif_tags['Image Orientation'].values[0] != 1
+
+
 def exif_fix_image_orientation(im, exif_tags):
     """
     Translate any EXIF orientation to raw orientation
@@ -42,7 +49,7 @@ def exif_fix_image_orientation(im, exif_tags):
     - REDUCES IMAGE QUALITY by recompressig it
 
     Pros:
-    - Cures my neck pain
+    - Prevents neck pain
     """
     # Rotate image
     if 'Image Orientation' in exif_tags:
@@ -57,6 +64,7 @@ def exif_fix_image_orientation(im, exif_tags):
 
     return im
 
+
 def extract_exif(filename):
     """
     Returns EXIF tags found in file at ``filename``
@@ -71,10 +79,10 @@ def extract_exif(filename):
 
     return exif_tags
 
+
 def clean_exif(exif):
     '''
-    Clean the result from anyt
-hing the database cannot handle
+    Clean the result from anything the database cannot handle
     '''
     # Discard any JPEG thumbnail, for database compatibility
     # and that I cannot see a case when we would use it.
@@ -92,6 +100,7 @@ hing the database cannot handle
 
     return clean_exif
 
+
 def _ifd_tag_to_dict(tag):
     data = {
         'printable': tag.printable,
@@ -113,9 +122,11 @@ def _ifd_tag_to_dict(tag):
 
     return data
 
+
 def _ratio_to_list(ratio):
     return [ratio.num, ratio.den]
 
+
 def get_useful(tags):
     useful = {}
     for key, tag in tags.items():
@@ -123,17 +134,17 @@ def get_useful(tags):
             useful[key] = tag
 
     return useful
-            
+
 
 def get_gps_data(tags):
     """
     Processes EXIF data returned by EXIF.py
     """
-    if not 'Image GPSInfo' in tags:
-        return False
-
     gps_data = {}
 
+    if not 'Image GPSInfo' in tags:
+        return gps_data
+
     try:
         dms_data = {
             'latitude': tags['GPS GPSLatitude'],
@@ -143,9 +154,13 @@ def get_gps_data(tags):
             gps_data[key] = (
                 lambda v:
                     float(v[0].num) / float(v[0].den) \
-                    + (float(v[1].num) / float(v[1].den) / 60 )\
+                    + (float(v[1].num) / float(v[1].den) / 60\
                     + (float(v[2].num) / float(v[2].den) / (60 * 60))
                 )(dat.values)
+
+        if tags['GPS GPSLongitudeRef'].values == 'W':
+            gps_data['longitude'] /= -1
+
     except KeyError:
         pass