seba6m6
seba6m6

Reputation: 119

Compressing the image using PIL without changing the orientation of the image

I want to find a way to compress the image and keep it in the same orientation. My code:

def save(self, **kwargs):
    super(Post, self).save()
    if self.picture:
        mywidth = 1100
        image = Image.open(self.picture)
        wpercent = (mywidth / float(image.size[0]))
        hsize = int((float(image.size[1]) * float(wpercent)))
        image = image.resize((mywidth, hsize), Image.ANTIALIAS)
        image.save(self.picture.path)

Even if i use just this bit:

image = Image.open(self.picture)

and then save it without doing anything

image.save(self.picture.path)

it still gives my the picture with the changed orientation...

Upvotes: 4

Views: 2364

Answers (2)

Csaba Toth
Csaba Toth

Reputation: 10709

What @radarhere referred is good, but instead of complicated manual orientation fiddling at least since Pillow 6.x there's the exif_transpose method: https://pillow.readthedocs.io/en/latest/_modules/PIL/ImageOps.html#exif_transpose

That performs a transformation (rotation, transpose, etc) on the image according to the EXIF orientation information and then adjusts the EXIF data by removing that piece and a bit more if needed, but basically preserving the rest of the EXIF.

This way the code is simply (and it can handle more orientation types than just 3, 6, 8):

from PIL import Image, ImageOps
image = Image.open(self.picture)
fixed_image = ImageOps.exif_transpose(image)

I think a resize after that will remove the EXIF since the size changes, but at that point the image will have already the proper orientation without the EXIF meta-data, so all is good.

Upvotes: 4

radarhere
radarhere

Reputation: 1039

I suspect that you're experiencing the same issue as PIL thumbnail is rotating my image?

PIL is not rotating the image as such. The image file has a flag noting the orientation of the image, which Pillow is reading, but not saving to your new file.

So I would try -

from PIL import Image, ExifTags

def save(self, **kwargs):
    super(Post, self).save()
    if self.picture:
        mywidth = 1100
        image = Image.open(self.picture)

        if hasattr(image, '_getexif'):
            exif = image._getexif()
            if exif:
                for tag, label in ExifTags.TAGS.items():
                    if label == 'Orientation':
                        orientation = tag
                        break
                if orientation in exif:
                    if exif[orientation] == 3:
                        image = image.rotate(180, expand=True)
                    elif exif[orientation] == 6:
                        image = image.rotate(270, expand=True)
                    elif exif[orientation] == 8:
                        image = image.rotate(90, expand=True)

        wpercent = (mywidth / float(image.size[0]))
        hsize = int((float(image.size[1]) * float(wpercent)))
        image = image.resize((mywidth, hsize), Image.ANTIALIAS)
        image.save(self.picture.path)

Upvotes: 4

Related Questions