You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have encountered two bugs with regards to image orientation. They both work together to make image thumbnailing create undesirable results for portrait images with exif orientation data, thus I am putting them together in the same issue.
Problem 1
The Engine.get_image_ratio function does not take into account orientation set in the exif data. As a result, the ratio returned for images with an orientation of 5, 6, 7 or 8 will have a landscape ratio returned instead of a portrait one.
Problem 2
The PIL engine should be using the transpose method instead of the rotate method when setting the image orientation. Using the rotate method causes a cropped image with black bars on either side.
To resolve the problem temporarily, I created my own engine with the above changes. I have included it below for reference.
from PIL import Image
from sorl.thumbnail.engines.pil_engine import Engine
class OutsidewaysEngine(Engine):
def _get_exif(self, image):
try:
exif = image._getexif()
except:
exif = None
return exif
def get_image_ratio(self, image, options):
ratio = super(OutsidewaysEngine, self).get_image_ratio(image, options)
exif = self._get_exif(image)
if exif:
orientation = exif.get(0x0112)
if orientation in [5, 6, 7, 8]:
ratio = 1.0 / ratio
return ratio
def _orientation(self, image):
exif = self._get_exif(image)
if exif:
orientation = exif.get(0x0112)
if orientation == 2:
image = image.transpose(Image.FLIP_LEFT_RIGHT)
elif orientation == 3:
image = image.transpose(Image.ROTATE_180)
elif orientation == 4:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
elif orientation == 5:
image = image.transpose(Image.ROTATE_270).transpose(
Image.FLIP_LEFT_RIGHT)
elif orientation == 6:
image = image.transpose(Image.ROTATE_270)
elif orientation == 7:
image = image.transpose(Image.ROTATE_90).transpose(
Image.FLIP_LEFT_RIGHT)
elif orientation == 8:
image = image.transpose(Image.ROTATE_90)
return image
The text was updated successfully, but these errors were encountered:
I have encountered two bugs with regards to image orientation. They both work together to make image thumbnailing create undesirable results for portrait images with exif orientation data, thus I am putting them together in the same issue.
Problem 1
The Engine.get_image_ratio function does not take into account orientation set in the exif data. As a result, the ratio returned for images with an orientation of 5, 6, 7 or 8 will have a landscape ratio returned instead of a portrait one.
Problem 2
The PIL engine should be using the transpose method instead of the rotate method when setting the image orientation. Using the rotate method causes a cropped image with black bars on either side.
To resolve the problem temporarily, I created my own engine with the above changes. I have included it below for reference.
The text was updated successfully, but these errors were encountered: