Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2 orientation bugs #495

Open
dtougas opened this issue May 5, 2017 · 0 comments
Open

2 orientation bugs #495

dtougas opened this issue May 5, 2017 · 0 comments

Comments

@dtougas
Copy link

dtougas commented May 5, 2017

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant