]> git.0d.be Git - django-panik-emissions.git/blobdiff - emissions/utils.py
remove LightSource exif data as it's broken on some phones
[django-panik-emissions.git] / emissions / utils.py
index 25cb093221c38e0db06f15e05b748803d3604c69..1d22a7abdc97d8134eebc2c44d97ae65308dbf37 100644 (file)
@@ -3,6 +3,7 @@ import re
 import subprocess
 from datetime import datetime, time, timedelta
 
+import piexif
 import taggit.utils
 from django.utils.encoding import force_str
 from PIL import Image
@@ -49,6 +50,18 @@ def maybe_resize(image_path):
     if not os.path.exists(image_path):
         return
     image = Image.open(image_path)
+
+    kwargs = {}
+    save_image = False
+
+    if 'exif' in image.info:
+        exif_dict = piexif.load(image.info['exif'])
+        if 0x9208 in exif_dict.get('Exif', {}):
+            # remove LightSource exif data as it's corrupt on some motorola phones
+            del exif_dict['Exif'][0x9208]
+            kwargs = {'exif': piexif.dump(exif_dict)}
+            save_image = True
+
     if max(image.size) > 1400:
         # no sense storing images that large
         factor = 1400.0 / max(image.size)
@@ -62,7 +75,10 @@ def maybe_resize(image_path):
             # make sure png files are always RGB
             # (it happened cmyk jpeg files were uploaded with a .png extension and that would fail)
             image = image.convert('RGB')
-        image.save(image_path)
+        save_image = True
+
+    if save_image:
+        image.save(image_path, **kwargs)
 
 
 def whatsonair(now=None):