Skip to content

FIX: Handle CMYK images when output format is PNG in image converters (#3257) - #3258

Merged
Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
Kaap10:fix/cmyk-image-converter-png
Oct 17, 2026
Merged

Roman Lutz (romanlutz) merged 4 commits into
microsoft:mainfrom
Kaap10:fix/cmyk-image-converter-png

Conversation

@Kaap10

Copy link
Copy Markdown
Contributor

Closes #3257

Description

BaseImageToImageConverter (which powers ImageResizingConverter) and ImageCompressionConverter support converting and compressing images across JPEG, PNG, and WEBP formats.

However, when processing an image in CMYK color mode (common in print media, scans, and press JPEGs) with output_format="PNG", Pillow's PNG encoder (PIL.PngImagePlugin) throws an uncaught KeyError: 'CMYK' wrapped in OSError: cannot write mode CMYK as PNG because PNG does not support CMYK.

  • In BaseImageToImageConverter._transform_image: format preparation was only performed for output_format == "JPEG". When output_format == "PNG", CMYK images were passed directly to transformed.save(buffer, "PNG").
  • In ImageCompressionConverter._compress_image: format preparation was also only performed for output_format == "JPEG". PNG targets with CMYK mode bypassed mode normalization and crashed upon image.save(compressed_bytes, output_format, **save_kwargs).

This change normalizes CMYK images to RGB before saving to PNG:

  • In BaseImageToImageConverter._transform_image: converts CMYK images using the existing self._convert_to_rgb_or_rgba(image) helper when output_format == "PNG".
  • In ImageCompressionConverter._compress_image: converts CMYK images to RGB via image.convert("RGB") when output_format == "PNG".
  • Native grayscale (L, LA, 1), paletted (P), and transparent (RGBA) PNGs remain completely untouched.

Tests and Documentation

  • Added unit tests:
    • tests/unit/converter/test_image_resizing_converter.py: test_image_resizing_converter_cmyk_to_png_conversion verifies CMYK to PNG conversion, resulting format PNG, mode RGB, and expected resized dimensions.
    • tests/unit/converter/test_image_compression_converter.py: test_image_compression_converter_cmyk_to_png_conversion verifies CMYK compression to PNG without raising OSError, yielding format PNG and mode RGB.
  • Verified test suite:
    • uv run python -m pytest tests/unit/converter/test_image_resizing_converter.py tests/unit/converter/test_image_compression_converter.py: 42 passed in 6.40s
    • Sibling image converter tests (saturation, rotation, overlay): 89 passed in 3.21s
  • Ran ruff check and ruff format --check clean.

@Kaap10

Copy link
Copy Markdown
Contributor Author

Normalizes CMYK images to RGB before saving to PNG in BaseImageToImageConverter and ImageCompressionConverter, preventing unhandled Pillow OSError crashes while preserving native grayscale and RGBA modes.

Ready for review!

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f1bae157-7a50-4003-9e2a-977b72f628d2
Preserve the existing RGB conversion while removing its stale CMYK profile. Cover profile removal, source metadata preservation, saved output, and valid RGB/RGBA profile preservation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f1bae157-7a50-4003-9e2a-977b72f628d2
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: f1bae157-7a50-4003-9e2a-977b72f628d2
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Oct 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 17, 2026
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Oct 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 17, 2026
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Oct 17, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Oct 17, 2026
@romanlutz
Roman Lutz (romanlutz) added this pull request to the merge queue Oct 17, 2026
Merged via the queue into microsoft:main with commit 2edd4bb Oct 17, 2026
64 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.

BUG: BaseImageToImageConverter and ImageCompressionConverter crash on CMYK images when output format is PNG

2 participants