summaryrefslogtreecommitdiff
path: root/chimere/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/models.py')
-rw-r--r--chimere/models.py37
1 files changed, 35 insertions, 2 deletions
diff --git a/chimere/models.py b/chimere/models.py
index 4f6d4e0..85d4055 100644
--- a/chimere/models.py
+++ b/chimere/models.py
@@ -20,7 +20,7 @@
"""
Models description
"""
-import os, datetime, re, string
+import os, datetime, pyexiv2, re, string
import simplejson as json
from lxml import etree
from PIL import Image
@@ -563,11 +563,44 @@ def scale_image(max_x, pair):
new_y = (float(max_x) / x) * y
return (int(max_x), int(new_y))
+
+IMAGE_EXIF_ORIENTATION_MAP = {
+ 1: 0,
+ 8: 2,
+ 3: 3,
+ 6: 4,
+}
+
+PYEXIV2_OLD_API = not hasattr(pyexiv2, 'ImageMetadata')
+
def picturefile_post_save(sender, **kwargs):
if not kwargs['instance']:
return
picturefile = kwargs['instance']
+ if kwargs['created']:
+ filename = picturefile.picture.path
+ metadata = None
+ if PYEXIV2_OLD_API:
+ metadata = pyexiv2.Image(filename)
+ metadata.readMetadata()
+ else:
+ metadata = pyexiv2.ImageMetadata(filename)
+ metadata.read()
+ orientation = metadata['Exif.Image.Orientation'].value \
+ if 'Exif.Image.Orientation' in metadata else None
+ print "orientation", orientation
+ if orientation and orientation in IMAGE_EXIF_ORIENTATION_MAP \
+ and orientation > 1:
+ metadata['Exif.Image.Orientation'] = 1
+ if PYEXIV2_OLD_API:
+ metadata.writeMetadata()
+ else:
+ metadata.write()
+ im = Image.open(filename)
+ im = im.transpose(IMAGE_EXIF_ORIENTATION_MAP[orientation])
+ im.save(filename)
+
if not picturefile.thumbnailfile:
file = picturefile.picture
# defining the filename and the thumbnail filename
@@ -604,7 +637,7 @@ def picturefile_post_save(sender, **kwargs):
picturefile.thumbnailfile = short_name
picturefile.save()
- if not kwargs['instance'] or not kwargs['created']:
+ if not kwargs['created']:
return
pfs = PictureFile.objects.filter(marker=picturefile.marker).exclude(
pk=picturefile.pk).order_by('order')