import piexif
import taggit.utils
from django.utils.encoding import force_str
-from PIL import Image
+from PIL import ExifTags, Image
from .app_settings import app_settings
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]
+ if exif_dict.get('GPS'):
+ exif_dict['GPS'] = {}
+ save_image = True
+ if 'thumbnail' in exif_dict:
+ del exif_dict['thumbnail']
+ save_image = True
+ for ifd in list(exif_dict.get('Exif', {}).keys()):
+ # remove all tags except width/height/orientation
+ # this is because some phone/apps create bogus metadata
+ # that would break basic things like rotation.
+ if ifd not in (
+ ExifTags.Base.ExifImageWidth,
+ ExifTags.Base.ExifImageHeight,
+ ExifTags.Base.Orientation,
+ ):
+ del exif_dict['Exif'][ifd]
+ save_image = True
+ if save_image:
try:
kwargs = {'exif': piexif.dump(exif_dict)}
except ValueError:
# really broken content, remove it all
kwargs = {'exif': piexif.dump({})}
- save_image = True
if max(image.size) > 1400:
# no sense storing images that large