Eric Bergman-Terrell's Blog

Python Tip: Fix Incorrect Orientation of Digital Photos
August 8, 2026

I sometimes rotate my phone to take photos. This causes the photo's EXIF metadata to have a rotation value other than 1. My phone is able to automatically orient these photos when they're displayed.

Unfortunately, my screen saver is not. Consequently a few of my ~44,000 photos render incorrectly. Fortunately the Python ImageOps library makes it easy to find such photos and fix them.

Here's the script I wrote to automatically check all my photos, and fix the ones that need it. Enjoy!

You'll need to change this line to point to the folder on your machine that holds your digital photos:

folder_path = Path('Z:/Photos/Photo Album')

Oh, just one more thing: ImageOps.exif_transpose is lossless.

import os
from pathlib import Path
from PIL import Image, ImageOps
from PIL.ExifTags import TAGS

ORIENTATION_TAG_ID = next(k for k, v in TAGS.items() if v == 'Orientation')

def main():
    folder_path = Path('Z:/Photos/Photo Album')

    for file_path in [f for f in folder_path.rglob("*") if f.is_file()]:
        path = os.path.abspath(file_path)

        if path.lower().endswith('.jpg') or path.lower().endswith('.jpeg'):
            img = Image.open(path)

            exif = img.getexif()

            if exif is not None:
                orientation = exif.get(ORIENTATION_TAG_ID)

                # If photo needs to be rotated
                if orientation is not None and orientation != 0 and orientation != 1:
                    print(f'fixing orientation: {orientation} "{path}"')

                    corrected_image = ImageOps.exif_transpose(img)
                    corrected_image.save(path)

if __name__ == '__main__':
    main()
Keywords: orientation, digital photographs, jpg, jpeg, Python, EXIF, rotation, ImageOps

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Python Tip: Fix Incorrect Orientation of Digital PhotosAugust 8, 2026
EBT Weather is now available for Windows and LinuxMay 30, 2026
Node.js + Express: How to Block Requests by User-Agent HeadersJanuary 7, 2026
Vault 3 is Now Available for Windows on ARM Machines!December 13, 2025
Vault 3: How to Include Outline Text in Exported PhotosOctober 26, 2025
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025