Fixed CMYK images crashing PNG encoding in PyPDF reader#1311
Merged
jamesbraza merged 1 commit intomainfrom Mar 4, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses crashes in the PyPDF-based reader when encountering images that Pillow cannot encode as PNG due to unsupported color modes (notably CMYK), by adding a safe fallback conversion to RGB and extending test coverage to reproduce the issue.
Changes:
- Add an RGB-conversion fallback when PNG encoding fails during individual image extraction.
- Extend
test_individual_mode_outputs_pngto include a CMYK input case that previously triggered the crash.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/paper-qa-pypdf/src/paperqa_pypdf/reader.py | Adds try/except around PNG encoding and falls back to RGB conversion on failure. |
| packages/paper-qa-pypdf/tests/test_paperqa_pypdf.py | Updates parametrized test to include a CMYK image case and verifies PNG output. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
923ad23 to
efed641
Compare
PNG doesn't support all color modes (e.g. CMYK from print-oriented PDFs). When Pillow raises OSError for an unsupported mode, fall back to converting the image to RGB before re-encoding as PNG. Made-with: Cursor
efed641 to
a0c25ca
Compare
sidnarayanan
approved these changes
Mar 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 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.
Closes #1310
Summary
test_individual_mode_outputs_pngthat reproduces the crashFollow-up to #1298, which handled file format re-encoding but missed color mode incompatibility.
Made with Cursor