Some photos get rotated!
Opened this issue · 3 comments
Deleted user commented
There's something odd with how exif data is processed: some photos are vertical but swipebox displays them rotated, for example this photo:
https://drive.google.com/file/d/0B8T6sxTm3685RWg3Wnota0xGX2s/view?usp=sharing
I'm using the following piece of code (python) to display the thumbnail with the correct rotation, and it rotates properly the image, I've never had any issue with it. Maybe can you get the inspiration from this snippet.
Thank you.
def Fix_image_rotation(image):
orientation_to_rotation_map = {
3: Image.ROTATE_180,
6: Image.ROTATE_270,
8: Image.ROTATE_90,
}
try:
exif = _get_exif_from_image(image)
orientation = _get_orientation_from_exif(exif)
rotation = orientation_to_rotation_map.get(orientation)
if rotation:
image = image.transpose(rotation)
except Exception as e:
pass
# Would like to catch specific exceptions, but PIL library is poorly documented on Exceptions thrown
# Log error here
finally:
return image
def _get_exif_from_image(image):
exif = {}
if hasattr(image, '_getexif'): # only jpegs have _getexif
exif_or_none = image._getexif()
if exif_or_none is not None:
exif = exif_or_none
return exif
def _get_orientation_from_exif(exif):
ORIENTATION_TAG = 'Orientation'
orientation_iterator = (
exif.get(tag_key) for tag_key, tag_value in ExifTags.TAGS.items()
if tag_value == ORIENTATION_TAG
)
orientation = next(orientation_iterator, None)
return orientation
Deleted user commented
It's been around 50 days, has anyone looked at this issue?
shawnCaza commented
Do you have a specific need for rotation to happen in javascript? Wouldn't it be more efficient to just rotate on upload?
Deleted user commented
The point is that photoswipe rotates pictures, but incorrectly.