Skip to content

Remove ICC profile from info when convert() changes the color space - #10683

Open
Yasser-Ameur wants to merge 2 commits into
python-pillow:mainfrom
Yasser-Ameur:fix/convert-drop-icc-profile
Open

Yasser-Ameur wants to merge 2 commits into
python-pillow:mainfrom
Yasser-Ameur:fix/convert-drop-icc-profile

Conversation

@Yasser-Ameur

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • Image.convert() no longer copies info["icc_profile"] to the new image when the conversion changes the color space, for example from L or CMYK to RGB. A grayscale or CMYK profile does not describe RGB pixels, and the PNG, TIFF and AVIF writers embed info["icc_profile"] when saving. ImageCms.profileToProfile error #6666 is the user-facing case: the profile of a converted image was passed to profileToProfile and failed to build a transform.
  • Conversions within a color space keep the profile, such as RGB to RGBA, RGB to P and L to LA. YCbCr and HSV count as RGB, since they are derived from RGB values.
  • This is narrower than removing the profile on every mode change. resize(), reduce() and transform() convert RGBA to RGBa and back through convert(), so dropping the profile on any mode change would strip it from every RGBA resize. The same applies to RGB to RGBA before pasting or saving PNG.
  • Two callers inside Pillow convert to another color space and back, and now keep their profile: ImageEnhance.Color (the degenerate image it blends from) and remap_palette (which converts P to L as an index trick, and now calls the conversion body directly).
  • Only a profile carried over from the source image is removed, so the profile that ImageCms sets when converting to or from LAB is kept. The source image's info is not modified.
  • Tests cover each case and a release note is added. If the maintainers prefer the simpler rule of removing the profile on any mode change, it is a small change to one condition, at the cost of the RGBA cases above.

Suggested by @radarhere in #10678.

Checked against the 13.0.0 release with the same change applied to its Python files: the eight new removal tests fail without it and pass with it, no other result changes in test_image.py, test_image_convert.py, test_imageenhance.py, test_imageops.py, test_file_gif.py, test_file_png.py and test_file_jpeg.py, and the bytes of an optimised GIF save are identical. The full suite on main is left to CI.

A grayscale or CMYK profile does not describe RGB pixels, but convert() copied it to the new image, and the PNG, TIFF and AVIF writers then embedded it. The profile is still kept for conversions within a color space, such as RGB to RGBA or P, and the profile ImageCms sets for LAB conversions is left alone.

ImageEnhance.Color converts to L and back to build its degenerate image, and remap_palette converts a P image to L as an index trick. Both now keep the profile of the source image.
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review December 1, 2026 13:17

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants