Skip to content

Commit

Permalink
fix Bug #2008.
Browse files Browse the repository at this point in the history
This bug was due to a very reduced exif set, which did not include ImageIFD.XResolution and ImageIFD.YResolution information.
  • Loading branch information
loechel committed Apr 14, 2017
1 parent 648a828 commit 0ba30c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Expand Up @@ -14,7 +14,8 @@ New features:

Bug fixes:

- *add item here*
- Fix bug on Image rotation if ImageIFD.XResolution or ImageIFD.YResolution are not set.
[loechel]


4.2.0 (2017-03-26)
Expand Down
31 changes: 24 additions & 7 deletions plone/namedfile/utils/__init__.py
Expand Up @@ -196,6 +196,11 @@ def rotate_image(image_data, method=None, REQUEST=None):
pass
if exif_data and piexif.ImageIFD.Orientation in exif_data['0th']:
orientation = exif_data['0th'][piexif.ImageIFD.Orientation]
if exif_data and \
(not exif_data['0th'].get(piexif.ImageIFD.XResolution) or \
not exif_data['0th'].get(piexif.ImageIFD.YResolution)):
exif_data['0th'][piexif.ImageIFD.XResolution] = (img.width, 1)
exif_data['0th'][piexif.ImageIFD.YResolution] = (img.height, 1)
if exif_data is None:
width, height = img.size
exif_data = {
Expand All @@ -219,19 +224,31 @@ def rotate_image(image_data, method=None, REQUEST=None):
elif orientation == 3:
img = img.transpose(PIL.Image.ROTATE_180)
elif orientation == 4:
img = img.transpose(PIL.Image.ROTATE_180).transpose(PIL.Image.FLIP_LEFT_RIGHT) # NOQA
img = img.transpose(PIL.Image.ROTATE_180).transpose(
PIL.Image.FLIP_LEFT_RIGHT)
elif orientation == 5:
img = img.transpose(PIL.Image.ROTATE_270).transpose(PIL.Image.FLIP_LEFT_RIGHT) # NOQA
exif_data['0th'][piexif.ImageIFD.XResolution], exif_data['0th'][piexif.ImageIFD.YResolution] = exif_data['0th'][piexif.ImageIFD.YResolution], exif_data['0th'][piexif.ImageIFD.XResolution] # NOQA
img = img.transpose(PIL.Image.ROTATE_270).transpose(
PIL.Image.FLIP_LEFT_RIGHT)
elif orientation == 6:
img = img.transpose(PIL.Image.ROTATE_270)
exif_data['0th'][piexif.ImageIFD.XResolution], exif_data['0th'][piexif.ImageIFD.YResolution] = exif_data['0th'][piexif.ImageIFD.YResolution], exif_data['0th'][piexif.ImageIFD.XResolution] # NOQA
elif orientation == 7:
img = img.transpose(PIL.Image.ROTATE_90).transpose(PIL.Image.FLIP_LEFT_RIGHT) # NOQA
exif_data['0th'][piexif.ImageIFD.XResolution], exif_data['0th'][piexif.ImageIFD.YResolution] = exif_data['0th'][piexif.ImageIFD.YResolution], exif_data['0th'][piexif.ImageIFD.XResolution] # NOQA
img = img.transpose(PIL.Image.ROTATE_90).transpose(
PIL.Image.FLIP_LEFT_RIGHT)
elif orientation == 8:
img = img.transpose(PIL.Image.ROTATE_90)
exif_data['0th'][piexif.ImageIFD.XResolution], exif_data['0th'][piexif.ImageIFD.YResolution] = exif_data['0th'][piexif.ImageIFD.YResolution], exif_data['0th'][piexif.ImageIFD.XResolution] # NOQA

if orientation in [5, 6, 7, 8]:
if exif_data['0th'][piexif.ImageIFD.XResolution] and \
exif_data['0th'][piexif.ImageIFD.YResolution]:
exif_data['0th'][piexif.ImageIFD.XResolution], \
exif_data['0th'][piexif.ImageIFD.YResolution] = \
exif_data['0th'][piexif.ImageIFD.YResolution], \
exif_data['0th'][piexif.ImageIFD.XResolution]
else:
exif_data['0th'][piexif.ImageIFD.XResolution], \
exif_data['0th'][piexif.ImageIFD.YResolution] = \
(img.width, 1), (img.height, 1)


# set orientation to normal
exif_data['0th'][piexif.ImageIFD.Orientation] = 1
Expand Down

1 comment on commit 0ba30c5

@jenkins-plone-org
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loechel Jenkins CI reporting about code analysis
See the full report here: http://jenkins.plone.org/job/package-plone.namedfile/33/violations

plone/namedfile/scaling.py:194:1: C901 'DefaultImageScalingFactory.__call__' is too complex (15)
plone/namedfile/testing.py:18:9: D001 found xmlconfig.file( replace it with self.loadZCML(
plone/namedfile/utils/__init__.py:179:1: C901 'rotate_image' is too complex (20)
plone/namedfile/utils/__init__.py:200:75: E502 the backslash is redundant between brackets
plone/namedfile/utils/__init__.py:253:5: E303 too many blank lines (2)

Follow these instructions to reproduce it locally.

Please sign in to comment.