Skip to content

Use ICC profile from info dictionary when saving WebP - #10678

Open
Yasser-Ameur wants to merge 1 commit into
python-pillow:mainfrom
Yasser-Ameur:fix/webp-icc-profile-from-info
Open

Yasser-Ameur wants to merge 1 commit into
python-pillow:mainfrom
Yasser-Ameur:fix/webp-icc-profile-from-info

Conversation

@Yasser-Ameur

Copy link
Copy Markdown
Contributor

Changes proposed in this pull request:

  • Saving an image as WebP drops its ICC profile unless icc_profile is passed. A JPEG with an embedded profile keeps it when it is saved as PNG, TIFF or AVIF, and loses it as WebP, so a wide gamut image is shown with the wrong colours after the conversion.
  • _save and _save_all now fall back to im.info.get("icc_profile") when no icc_profile argument is given, as the PNG, TIFF and AVIF plugins do.
  • The fallback is only used for modes that are saved without a colour conversion (RGB, RGBA, RGBX, P and PA). An L or CMYK image is converted to RGB before it is encoded, and a grayscale or CMYK profile would not describe the result.
  • An explicit icc_profile argument behaves as before, and icc_profile=None still saves without a profile.
  • Tests in Tests/test_file_webp_metadata.py cover a still and an animated save, and a grayscale image whose profile must not be written. A release note is added to docs/releasenotes/13.1.0.rst.
import io

from PIL import Image

with Image.open("Tests/images/flower2.jpg") as im:
    print("source profile:", len(im.info["icc_profile"]))
    for fmt in ("PNG", "TIFF", "WEBP"):
        out = io.BytesIO()
        im.save(out, fmt)
        with Image.open(out) as reloaded:
            print(fmt, len(reloaded.info.get("icc_profile", b"")))

On 13.0.0 this prints 3144 for the source, PNG and TIFF, and 0 for WEBP.

When no icc_profile argument is passed, the profile is now taken from the
image's info dictionary, as PNG, TIFF and AVIF do. This is only done for
modes that are saved without a colour conversion (RGB, RGBA, RGBX, P and
PA), so a grayscale or CMYK profile is never written next to RGB pixels.
icc_profile=None still writes no profile.
@radarhere

Copy link
Copy Markdown
Member

Hi. Just for our awareness, are you using AI to create these PRs?

@radarhere radarhere added the WebP label Nov 30, 2026
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review November 30, 2026 22:37
@radarhere

Copy link
Copy Markdown
Member

An L or CMYK image is converted to RGB before it is encoded, and a grayscale or CMYK profile would not describe the result.

This makes me think that we should be dropping the icc_profile on conversion?

>>> from PIL import Image
>>> im = Image.new("L", (1, 1))
>>> im.info["icc_profile"] = "test"
>>> im.convert("RGB").info
{'icc_profile': 'test'}

Would you be interested in opening a new PR for that?

@akx

akx commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Hi. Just for our awareness, are you using AI to create these PRs?

I would imagine they are, as python-babel/babel#1524 on Babel (which I maintain) looks very AI :) (Just happened to spot the same username over there...)

@radarhere radarhere added the 🤖-assisted AI-assisted label Dec 1, 2026
@Yasser-Ameur

Copy link
Copy Markdown
Contributor Author

Yes, these are AI assisted, reviewed and tested by me. Each one is reproduced on the released version and run against a new test before it is sent. The same goes for the Babel PR. If you'd prefer fewer of these, or a different form, tell me and I'll follow that.

@Yasser-Ameur

Copy link
Copy Markdown
Contributor Author

Yes, I'll open a separate PR that drops icc_profile from info when convert() changes the mode.

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.

3 participants