Uh oh!
There was an error while loading. Please reload this page.
Add support for image orientation - #1575
Open
TheZ3ro wants to merge 1 commit into
Open
Conversation
… from Exif data then fallback to normal orientation.
This was referenced Aug 4, 2026
TheZ3ro
commented
Aug 4, 2026
Author
script to reproduce/test the behavior: fromdocximportDocumentfromPILimportImage, ImageDraw# Draw a black arrow shaft and arrowheadimg=Image.new("RGB", (300, 400), "white")
d=ImageDraw.Draw(img)
cx, shaft_top, shaft_h=150, 50, 200d.rectangle([(145, shaft_top), (155, shaft_top+shaft_h)], fill="black")
d.polygon([(150, 30), (130, 70), (170, 70)], fill="black")
# Save the imageimg.save("arrow.jpg")
# Create a document and add the imagedocument=Document()
document.add_picture("arrow.jpg")
document.save("arrow_up.docx")
# Rotate the image only by changing the EXIF orientation, then save itimg=Image.open("arrow.jpg")
exif=img.getexif()
exif[0x0112] =6img.save("arrow_rotated.jpg", exif=exif)
# Create a document and add the rotated imagedocument=Document()
document.add_picture("arrow_rotated.jpg")
document.save("arrow_rotated.docx") |
This was referenced Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes#540
Modern image formats (PNG, JPEG, Tiff, etc) embed the orientation in Exif metadata.
When rotating or displaying an image, software like Word, PowerPoint, macOS Preview, Android Google Photo, etc do not rotate the actual pixel data but instead change the orientation metadata integer (https://exifstrip.com/guides/orientation-tag-explained)
This is governed in OpenXML with the
a:xfrm(transform) tag. This tag can contain therot,flipV, andflipHattributes. By mapping the 1-8 orientation values to the respective rot and flip attributes it's possible to display images that were rotated by merely changing the Exif metadata with the correct orientation.This PR add support for image orientation.
By default it gets orientation from Exif metadata. If orientation metadata is not available it falls back to "Normal" orientation (like now, defined by raw pixel data).
It is also possible to force a given orientation programmatically by: