Skip to content

fix: apply the EXIF orientation when loading document images - #5246

Merged
PeterStaar-IBM merged 1 commit into
docling-project:mainfrom
sohailshk:fix/image-loader-exif-orientation
Nov 9, 2026
Merged

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

Conversation

@sohailshk

Copy link
Copy Markdown
Contributor

A camera stores the sensor readout and an EXIF orientation tag, not rotated pixels, and browsers show an <img> turned by that tag. ImageResourceLoader.create_image_ref() and the MHTML branch of HTMLDocumentBackend ignored the tag. A portrait photo in an HTML, MHTML, Markdown, AsciiDoc, JATS or EPUB document therefore became a sideways picture with its width and height swapped. ImageDocumentBackend already applies the tag (#4247).

This change calls ImageOps.exif_transpose after the image is decoded, in both places, as #4247 does for image pages. The DPI read after it is unchanged: exif_transpose keeps info.

Issue resolved by this Pull Request:
Resolves #5245

Checklist:

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

Review evidence:

Source Before After
200x100 JPEG, black top-left corner, Orientation=6, as <img> data URI (HTML) and ![](...) (Markdown), fetch_images=True picture 200x100, corner at the top left picture 100x200, corner at the top right
Same JPEG as a cid: part of an MHTML archive 200x100 100x200
  • test_create_image_ref_applies_exif_orientation in tests/test_image_resource_loader.py covers the shared loader (HTML, Markdown, AsciiDoc, JATS and EPUB use it).
  • test_embedded_image_follows_exif_orientation in tests/test_backend_mhtml.py covers the MHTML branch, which decodes on its own.
  • Both tests fail on main (assert (200, 100) == (100, 200)) and pass with the change.
  • pytest tests/test_image_resource_loader.py tests/test_backend_mhtml.py tests/test_backend_html.py tests/test_backend_markdown.py tests/test_backend_markdown_images.py tests/test_backend_asciidoc.py tests/test_backend_jats.py tests/test_backend_jats_sub_articles.py tests/test_backend_jats_table_footnotes.py tests/test_backend_epub.py: 567 passed, 2 skipped.
  • prek run --files <changed files> (what make validate runs): all hooks passed (Ruff v0.17.2, Ruff format, ty 0.0.33, Tach, license header, max lines).
  • No reference data changes: no test document has an EXIF-rotated image.
  • Environment: macOS 26.6 (arm64), Python 3.13.3, Pillow 13.0.0, docling-core 2.106.0.

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 (docling-project#4247).

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

Signed-off-by: Mohammad Sohail Shaikh <[email protected]>
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

✅ DCO Check Passed

Thanks @sohailshk, all your commits are properly signed off. 🎉

@mergify

mergify Bot commented Nov 8, 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)(?:\(.+\))?(!)?:

@github-actions github-actions Bot added ai:ci-safe AI triage found no CI-safety concern (advisory) bug Something isn't working html issue related to html backend labels Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

AI triage (advisory)

Checked commit 1993454407. The analysis read the diff only. It did not run code from this pull request.

This pull request applies the EXIF orientation tag when ImageResourceLoader or HTMLDocumentBackend decodes document images. It adds unit tests for MHTML embedded images and the shared image resource loader.

Topic labels added: bug, html.

CI safety: no concern found. A maintainer must still approve CI.

2 related pull request(s) in the same code
PR State Signal Assessment
#5082 open changes the same code related: PR #5082 modifies path resolution for relative image URLs in the same files, but it does not handle EXIF orientation.
#4247 merged referenced as #4247 related: PR #4247 applied EXIF orientation to image pages in ImageDocumentBackend, while this pull request applies EXIF orientation to images loaded by ImageResourceLoader and HTMLDocumentBackend.

The ai:* labels are advisory. They do not approve CI and they do not add tests:full. A new push runs this check again.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI first review (advisory): no blocker found

This review covers the full diff at 1993454407. It read the code and did not run it.

I found no merge-blocking issue. ImageOps.exif_transpose is called after img.load(), so the pixel-limit check still runs before decode. It is called before the DPI read and normalize_image_for_png. The helper ImageResourceLoader.create_image_ref() and the MHTML branch in _create_image_ref are the only Image.open sites in the HTML-family paths. Both tests assert the swapped size and the corner position, so they check the defect.

A maintainer makes the merge decision. This review never approves the pull request.

@github-actions github-actions Bot added the ai:review-lgtm AI first review found no blocker or major issue (advisory) label Nov 8, 2026
@codecov

codecov Bot commented Nov 9, 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 550cba5 into docling-project:main Nov 9, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai:ci-safe AI triage found no CI-safety concern (advisory) ai:review-lgtm AI first review found no blocker or major issue (advisory) bug Something isn't working html issue related to html backend

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML and Markdown images ignore the EXIF orientation, so portrait photos become sideways pictures

2 participants