Skip to content

Studio: apply EXIF orientation to uploaded images - #10553

Merged
Lyxot merged 6 commits into
unslothai:mainfrom
NilayYadav:fix-exif-orientation-uploaded-images
Sep 9, 2026
Merged

Lyxot merged 6 commits into
unslothai:mainfrom
NilayYadav:fix-exif-orientation-uploaded-images

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Sep 8, 2026 •

Copy link
Copy Markdown
Collaborator

A photo taken on a phone carries a rotation tag. The browser preview honours it, but the backend decoded the raw pixels, so Transform, Edit, Inpaint and video keyframes worked on a sideways image while the user saw it upright.

Inpaint was the worst case. The mask is drawn at the preview size, so it arrived transposed against the source and the model repainted a different part of the picture than the one the user brushed.

The decoder now applies the orientation tag in place, the same way the training loaders already do. Images without a tag are untouched.

The tests drive real rotated JPEGs through the decoder and check all eight orientation values against a table read off the tag definitions, asserting the decoded size and which stored quadrant lands in each display corner. Four of the eight are mirrors, so a fix that rotated where it should flip does not pass on sizes alone. They also pin the two properties around the change: the tag never decides whether an image is accepted, and an oversized image is still refused from the header before its pixels are read.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 8, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-09T10:35:50.836729Z 802ce33 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 8b1674f3dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@NilayYadav

NilayYadav commented Sep 8, 2026 •

Copy link
Copy Markdown
Collaborator Author

Review + GitHub Actions reproduction

Pinned head: 8b1674f3dd — no blocking code findings. Codex reviewed the same head and found no major issues.

The defect and the fix are reproduced by an A/B job on fresh upstream main (run 34285183578, ubuntu-latest, Python 3.12, pillow==12.3.0 as studio/backend/requirements/no-torch-runtime.txt pins it). Both halves run the identical probe and the identical test file; the only thing that changes between them is the one line under test.

The probe feeds decode_b64_image a phone photo — pixels stored 64×32 landscape, EXIF Orientation=6 saying "show me rotated" — plus the inpaint mask as the canvas emits it, sized from the preview the browser actually shows.

without the fix with the fix
stored JPEG size on the wire (64, 32) (64, 32)
browser preview shows (32, 64) (32, 64)
decode_b64_image returns (64, 32) (32, 64)
quadrants TL,TR,BL,BR ['red', 'green', 'blue', 'yellow'] ['blue', 'red', 'yellow', 'green']
browser-truth quadrants ['blue', 'red', 'yellow', 'green'] ['blue', 'red', 'yellow', 'green']
inpaint mask size (32, 64) (32, 64)
mask aligns with source False True

The mask row is the inpaint case: the mask arrives at the preview's size and the source did not, so the brushed region landed on a different part of the picture.

tests/test_diffusion_exif_orientation.py on the same runner:

  • fix removed: 2 failed, 9 passed, failing at exactly the orientation assertions (assert (64, 32) == (32, 64) and assert (32, 64) == (64, 32)), not at setup.
  • fix present: 11 passed in 0.18s.

The size guard reads the header dimensions before the transpose, so it is unchanged by this: the parametrised case asserts all eight orientation values decide max_side and max_pixels the same way, and a portrait photo is not refused at a size its landscape twin is accepted at.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 8b1674f3dd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 349d835e4e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 349d835e4e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 0c2cb4efec

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 8, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 0c2cb4efec

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 9, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Studio UI evidence: before / after

Two isolated Studio installs on one A10G, base 580cffaba (this PR's merge base) against head 0c2cb4efe. Same Z-Image-Turbo GGUF, same seed, same 1024x1024 Resolution box, same strength; the only difference between the two sides is this diff.

The upload is a four-colour card whose pixels are stored 768x448 landscape with EXIF Orientation=6 — a phone photo. The browser shows it 448x768, and Transform takes its output size from the upload.

Studio Images viewer, before and after

Every fact that moved, and nothing else:

fact BEFORE AFTER
stored_pixels_on_the_wire [768, 448] [768, 448]
browser_preview_size [448, 768] [448, 768]
gallery_recorded_size [768, 448] [448, 768]
result_pixel_size [768, 448] [448, 768]
result_is_portrait False True
result_quadrants ['red', 'green', 'blue', 'yellow'] ['blue', 'red', 'yellow', 'green']
result_matches_browser_truth False True
gallery_workflow img2img img2img

BEFORE, the run generated at the size of the stored pixels and in the stored quadrant order — the orientation nobody saw. AFTER it matches the preview.

The quadrant read is deliberately mirror-sensitive. Orientation 5 is the diagonal flip of orientation 6 and produces the same sizes, so a size-only check cannot tell a rotation from a mirror; the colours can.

Generated pixels, before and after

@Lyxot Lyxot self-assigned this Sep 9, 2026
Orientation was applied with ImageOps.exif_transpose, which resolves the tag through Image.getexif(). That also recovers orientation from XMP tiff:Orientation and from ImageMagick's hex text profile, neither of which Chromium applies, and it may have cached one of those while open() read the DPI, so stripping the metadata afterwards is too late. A file the preview left upright then decoded rotated, which lands an inpaint mask transposed against its own source and repaints a region the user never brushed.

The orientation is now read directly out of the EXIF block and applied from an explicit table, so no fallback source and no cache can reach it. A malformed block no longer propagates out of the tag parse, which had turned a previously accepted upload into a 400. WebP is skipped because Chromium does not orient it while WebKit does, leaving a known macOS divergence that no single server-side rule can close. Every orientation source is dropped after a transpose so a re-save cannot apply it twice.

Tests drive real files through the decoder: all eight tag values against a table read off the tag definitions rather than off Pillow, both metadata fallbacks, the orientation cached during open(), a malformed block, a PNG eXIf chunk, WebP, and the size guard's ordering against the transpose.
@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Pushed a commit to this branch (d24e847) — thanks for the fix, the bug and the inpaint diagnosis are both right. Reviewing it turned up four cases where the tag was resolved differently from the preview, so the orientation is now read from the EXIF block directly instead of through ImageOps.exif_transpose.

What changed and why:

  • getexif() reads more than the browser does. It also recovers orientation from XMP tiff:Orientation and from ImageMagick's Raw profile type exif, neither of which Chromium applies. On such a file the decode rotated an image the preview left upright — the same transposed mask, reached from the other side.
  • It can also have cached one of those before you can strip it. For a JPEG whose EXIF block carries resolution but no Orientation, the opener reads the DPI during open() and fills its EXIF cache from the XMP fallback, so clearing img.info afterwards is too late.
  • A malformed EXIF block became a 400. A PNG whose eXIf payload is not a TIFF header raises out of the tag parse, and the function's except Exception turned an upload that previously decoded fine into a decode error.
  • WebP is skipped. Measured: Chromium does not apply EXIF orientation to WebP; WebKit does. Desktop is Tauri (WebView2/WKWebView/WebKitGTK), so no server-side rule fits every engine — this matches Chromium and leaves a known, commented divergence on macOS for the rare WebP that carries an orientation tag.

Every orientation source is also dropped after a transpose, so a re-save can't apply it twice.

Your test file is kept and extended — the eight-orientation table and the mirror-vs-rotation fixture were what caught most of this, and a rotate-only fix still fails orientations 2, 4, 5 and 7. Added cases for both metadata fallbacks, the cached-orientation path, the malformed block, a PNG eXIf chunk, and WebP. Each was checked by mutation: eleven variants of the wrong fix, each failing exactly the test that should catch it and no other. Browser behaviour was measured directly rather than assumed — ten files rendered and compared against the decoder.

Two things deliberately left alone, both worth separate PRs:

  • Non-conforming Orientation field types (LONG, or SHORT with count 2) are ignored by Chromium but applied here. Image.Exif exposes no raw field type or count, so enforcing the spec's SHORT/count-1 would mean hand-parsing the TIFF IFD in the decode path — a refusal whose failure mode is ignoring orientation on real photos.
  • The inpaint branch resizes a mismatched mask onto the source unconditionally, with no aspect check. That's what makes every one of these divergences silent instead of an error, and it's reachable without any orientation tag at all (a stale mask after swapping the source image).

Also worth noting the title now over-promises slightly: the chat and VLM upload paths (worker._decode_image, _decode_and_resize_image, _image_bytes_to_png_b64) still ignore orientation, so a phone photo pasted into chat still reaches the model sideways.

@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: d24e847659

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot

Lyxot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 802ce334ee

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot Lyxot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approving. The bug is real: the mask canvas is sized from naturalWidth/naturalHeight, which the browser has already orientation-corrected, so a phone photo with a rotation tag reached the backend as an unrotated buffer and the mask no longer lined up with it.

The fix reads the EXIF block directly rather than through getexif(), which is the right call — getexif() also recovers orientation from XMP and ImageMagick text profiles that no browser honours, and can hand back a value it cached during open(). Reading info["exif"] alone keeps the backend agreeing with what the user saw. The WebP skip and the metadata cleanup after the transpose are both correct and both covered.

Tests pin all eight orientation values against expected size and corner colours, plus the XMP-only, EXIF-vs-XMP conflict, text-profile, malformed-block and double-apply cases. Verified locally, including that the transpose runs after the size guard.

Two things left for follow-up, neither blocking here: the chat/VLM upload paths do not go through decode_b64_image and still ignore orientation, and the inpaint mask is still resized unconditionally when it does not match the source.

@Lyxot
Lyxot merged commit 7d5ef60 into unslothai:main Sep 9, 2026
31 of 34 checks passed
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.

2 participants