Skip to content
Permalink
Browse files
Improved EXIF orientation handling
  • Loading branch information
ralsina committed Jul 18, 2016
1 parent de12ca9 commit 2b1ec3d9ff7748d1fc270bd74ba49748afe5cb41
Showing with 27 additions and 18 deletions.
  1. +1 −0 CHANGES.txt
  2. +25 −18 nikola/image_processing.py
  3. +1 −0 requirements.txt
@@ -12,6 +12,7 @@ Features
Bugfixes
--------

* Improved EXIF orientation handling
* Register post list template as a dependency (Issue #2391)
* Fix section color hashing when using Python 2
* Use ``en_US`` dictionary name with pyphen for better compatibility
@@ -37,14 +37,16 @@

Image = None
try:
from PIL import Image, ExifTags # NOQA
from PIL import ExifTags, Image, ImageOps # NOQA
except ImportError:
try:
import Image as _Image
import ExifTags
import Image as _Image
import ImageOps
Image = _Image
except ImportError:
pass
import piexif


class ImageProcessor(object):
@@ -67,26 +69,31 @@ def resize_image(self, src, dst, max_size, bigger_panoramas=True, preserve_exif_
size = min(w, max_size * 4), min(w, max_size * 4)

try:
exif = im._getexif()
except Exception:
exif = piexif.load(im.info["exif"])
except KeyError:
exif = None
_exif = im.info.get('exif')
# Inside this if, we can manipulate exif as much as
# we want/need and it will be preserved if required
if exif is not None:
for tag, value in list(exif.items()):
decoded = ExifTags.TAGS.get(tag, tag)

if decoded == 'Orientation':
if value == 3:
im = im.rotate(180)
elif value == 6:
im = im.rotate(270)
elif value == 8:
im = im.rotate(90)
break
# Rotate according to EXIF
value = exif['0th'].get(piexif.ImageIFD.Orientation, 1)
if value in (3, 4):
im = im.rotate(180)
elif value in (5, 6):
im = im.rotate(270)
elif value in (7, 8):
im = im.rotate(90)
if value in (2, 4, 5, 7):
im = ImageOps.mirror(im)
exif['0th'][piexif.ImageIFD.Orientation] = 1
try:
im.thumbnail(size, Image.ANTIALIAS)
if _exif is not None and preserve_exif_data:
im.save(dst, exif=_exif)
if exif is not None and preserve_exif_data:
# Put right size in EXIF data
w, h = im.size
exif["0th"][piexif.ImageIFD.XResolution] = (w, 1)
exif["0th"][piexif.ImageIFD.YResolution] = (h, 1)
im.save(dst, exif=piexif.dump(exif))
else:
im.save(dst)
except Exception as e:
@@ -14,3 +14,4 @@ setuptools>=5.4.1
natsort>=3.5.2
requests>=2.2.0
husl>=4.0.2
piexif==1.0.4

0 comments on commit 2b1ec3d

Please sign in to comment.