fix: preserve image EXIF orientation and PDF /Rotate through compression#154
Merged
Merged
Conversation
Ghostscript's pdfwrite device silently strips /Rotate page attributes, leaving scanned PDFs displayed upside-down after compression. Pillow similarly drops EXIF Orientation when re-saving, rotating scanned images. file_processing.py: - Add ImageOps.exif_transpose() after Image.open() in both compress_image_upload and compress_image_inplace so scanner EXIF orientation is baked into pixels before the tag is stripped - compress_image_inplace now also detects orientation-only fixes so small files below the skip threshold are still corrected - Add _read_page_rotations() / _restore_page_rotations() helpers that snapshot page /Rotate values before Ghostscript and write them back into the output via pypdf; wired into both _compress_pdf_bytes (upload path) and compress_pdf_inplace (management command path) compress_media.py: - --force flag: bypass image size threshold (remediate already-compressed images with lost EXIF orientation) - --rotate-pages DEGREES flag: set /Rotate on every page of all PDFs under --path; accepts both a directory and a single file path (remediate PDFs whose rotation metadata was already stripped) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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 free
to 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.
Summary
pdfwritedevice silently strips/Rotatepage attributes from PDFs; Pillow drops the EXIFOrientationtag when re-saving images. Both compression paths (upload-time andcompress_mediamanagement command) were affected, leaving scanned documents displayed upside-down._read_page_rotations()/_restore_page_rotations()helpers that snapshot page/Rotatevalues before Ghostscript runs and write them back into the output via pypdf. Wired into both_compress_pdf_bytes(upload path) andcompress_pdf_inplace(management command).ImageOps.exif_transpose()afterImage.open()in bothcompress_image_uploadandcompress_image_inplaceso scanner EXIF orientation is baked into pixels before the tag is stripped on re-save.compress_mediamanagement command:--force: bypass image size threshold to reprocess already-compressed images (fixes lost EXIF orientation)--rotate-pages DEGREES [--path <file-or-dir>]: set/Rotateon every page of matching PDFs; accepts a single file or a directory (fixes PDFs whose rotation metadata was already stripped)Test plan
compress_image_inplace--rotate-pages 180 --path clients/ml, generated PDF individually corrected via--rotate-pages 0 --path clients/ml/2026/<file>.pdf🤖 Generated with Claude Code