Skip to content

fix(image): apply the EXIF orientation when loading image frames - #4247

Merged
PeterStaar-IBM merged 1 commit into
docling-project:mainfrom
kakiuwang-ui:fix/image-exif-orientation
Sep 16, 2026
Merged

PeterStaar-IBM merged 1 commit into
docling-project:mainfrom
kakiuwang-ui:fix/image-exif-orientation

Conversation

@kakiuwang-ui

Copy link
Copy Markdown
Contributor

A camera stores the sensor readout plus an EXIF orientation tag rather than
rotated pixels. ImageDocumentBackend never read that tag, so a portrait photo
reached OCR and layout on its side:

>>> # 200x100 JPEG tagged Orientation=6, i.e. "rotate a quarter turn"
>>> page.get_size()
Size(width=200.0, height=100.0)     # expected 100 x 200

The page image handed downstream is sideways too, so OCR reads a rotated line,
layout scores a rotated block, and the page image in the exported document is
rotated for the reader.

Implementation

_oriented_frame runs ImageOps.exif_transpose on each frame and returns it
with its DPI axes.

exif_transpose swaps the pixel dimensions for the quarter-turn orientations
but leaves info["dpi"] alone, so the axes are swapped here as well; without
that, a 300x150 frame at 300x150 dpi — a square 72x72 pt page — reads as
36x144 after the rotation.

The swap keys off the frame actually changing size rather than off the tag
value. Pillow's TIFF plugin already applies the orientation while opening, so
exif_transpose is a no-op there; keying off the tag would swap the DPI axes a
second time for those files.

Test

Two cases in tests/test_backend_image_native.py, both on a JPEG with a black
marker in the top-left corner:

  • test_exif_orientation_rotates_the_frame — the page is 100x200 and the
    marker has moved to the top-right.
  • test_exif_orientation_swaps_dpi_axes — the physical page stays 72x72 pt.

Checked by breaking each part: dropping the transpose fails both, and keeping
the transpose while dropping the DPI swap fails only the second. make validate
is clean and the file's other 25 tests still pass.

Notes for review

  • One corner this does not cover: a square frame with asymmetric DPI and a
    quarter-turn tag. The transpose leaves the size unchanged, so the DPI axes are
    not swapped. Keying off the tag instead would fix that case and break the TIFF
    one above; happy to handle both explicitly if you would rather.
  • No issue reference — found reading the backend against the EXIF spec, in the
    same shape as the recent fix(webvtt) and fix(epub) changes.

Checklist:

  • Documentation has been updated, if necessary.
  • Examples have been added, if necessary.
  • Tests have been added, if necessary.

A camera stores the sensor readout plus an orientation tag rather than
rotated pixels, so a portrait photo reached OCR and layout on its side: a
200x100 JPEG tagged orientation 6 came out 200x100 and unrotated instead
of 100x200.

ImageOps.exif_transpose swaps the pixel dimensions but leaves info["dpi"]
alone, so the DPI axes are swapped whenever it actually rotated the frame;
without that a square 72x72 pt page reads as 36x144.

Signed-off-by: kakiuwang-ui <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @kakiuwang-ui, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🟢 Merge protection satisfied — ready to merge.

Show 1 satisfied protection

🟢 Enforce conventional commit

Make sure that we follow https://www.conventionalcommits.org/en/v1.0.0/

  • title ~= ^(fix|feat|docs|style|refactor|perf|test|build|ci|chore|revert)(?:\(.+\))?(!)?:

@codecov

codecov Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@PeterStaar-IBM PeterStaar-IBM left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@PeterStaar-IBM
PeterStaar-IBM merged commit 77ab16d into docling-project:main Sep 16, 2026
26 checks passed
PeterStaar-IBM pushed a commit that referenced this pull request Nov 9, 2026
A camera stores the sensor readout and an EXIF orientation tag rather than
rotated pixels, and browsers show an <img> turned by that tag. The shared
ImageResourceLoader and the MHTML branch of the HTML backend ignored it, so a
portrait photo in an HTML, MHTML, Markdown, AsciiDoc, JATS or EPUB document
became a sideways picture with its width and height swapped. The image backend
already applies the tag (#4247).

Apply ImageOps.exif_transpose after the image is decoded in both places.

Signed-off-by: Mohammad Sohail Shaikh <[email protected]>
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

Successfully merging this pull request may close these issues.

3 participants