Home > Blockchain >  Rotate native iPhone images based on EXIF info?
Rotate native iPhone images based on EXIF info?

Time:04-27

Working on a site where users can upload photos. As many of you many know, unedited iPhone photos use an "Orientation" EXIF tag to determine orientation. On my PC, I can't upload .HEIC files, and I can't upload files if I do manage.py runserver localhostIP:8000 due to SSL reasons, so it's pretty difficult to debug. Looking at the EXIF data on iPhone photos it seems like this should rotate the iPhone images properly, but they still upload with the incorrect orientation, and I can't really look at the what EXIF tags iPhone pictures are returning in the backend.

im = Image.open(self.content_media)
img_exif = im.getexif()
for key, val in img_exif.items():
    print(val)
    if "rotate 90 cw" in str(val).lower():
        im.rotate(90)
    if "rotate 180 cw" in str(val).lower():
        im.rotate(180)
    if "rotate 270 cw" in str(val).lower():
        im.rotate(270)

This is in the save method for one of my models that has media.

CodePudding user response:

from PIL import Image, ImageOps

image = Image.open(foo)
im = ImageOps.exif_transpose(image)

Then you can do whatever compression/resizing/renaming or whatever you were using PIL for, but native iPhone and Samsung photos will have the proper rotation applied.

  • Related