Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot emit sRGB-chunk #2782

Closed
stuxcrystal opened this issue Oct 3, 2017 · 1 comment
Closed

Cannot emit sRGB-chunk #2782

stuxcrystal opened this issue Oct 3, 2017 · 1 comment

Comments

@stuxcrystal
Copy link
Contributor

stuxcrystal commented Oct 3, 2017

What did you do?

I tried to add an sRGB-chunk via PngInfo to the PNG image. (Script at the bottom).

What did you expect to happen?

File: /mnt/c/Users/stuxc/Desktop/Download_srgb3.png (1045 bytes)
  chunk IHDR at offset 0x0000c, length 13
    640 x 480 image, 24-bit RGB, non-interlaced
  chunk sRGB at offset 0x00025, length 1
    rendering intent = absolute colorimetric
  chunk gAMA at offset 0x00032, length 4: 0.45455
  chunk cHRM at offset 0x00042, length 32
    White x = 0.3127 y = 0.329,  Red x = 0.64 y = 0.33
    Green x = 0.3 y = 0.6,  Blue x = 0.15 y = 0.06
  chunk IDAT at offset 0x0006e, length 915
    zlib: deflated, 32K window, default compression
  chunk IEND at offset 0x0040d, length 0
No errors detected in /mnt/c/Users/stuxc/Desktop/Download_srgb3.png (6 chunks, 99.9% compression).

Note the sRGB chunk at offset 0x00025

What actually happened?

File: /mnt/c/Users/stuxc/Desktop/Download_srgb5.png (1032 bytes)
  chunk IHDR at offset 0x0000c, length 13
    640 x 480 image, 24-bit RGB, non-interlaced
  chunk gAMA at offset 0x00025, length 4: 0.45455
  chunk cHRM at offset 0x00035, length 32
    White x = 0.3127 y = 0.329,  Red x = 0.64 y = 0.33
    Green x = 0.3 y = 0.6,  Blue x = 0.15 y = 0.06
  chunk IDAT at offset 0x00061, length 915
    zlib: deflated, 32K window, default compression
  chunk IEND at offset 0x00400, length 0
No errors detected in /mnt/c/Users/stuxc/Desktop/Download_srgb5.png (5 chunks, 99.9% compression).

Both outputs were created by pngcheck -vt <Filename>

What versions of Pillow and Python are you using?

Pillow==4.3.0 affected code is also in the current master branch.
Python 3.6.2 using Windows Subsystem for Linux (Windows 10 Build 15063)

I think the error is located at:
https://github.com/python-pillow/Pillow/blob/master/PIL/PngImagePlugin.py#L732
This removes the sRGB-chunk from the list of valid chunks if (and only if) there is no iCCP-chunk given to the save function.

Script that replicates the bug

import struct
from PIL import Image
from PIL.PngImagePlugin import PngInfo

GAMA_SRGB = struct.pack('!I', 45455)
CHRM_SRGB = struct.pack("!8I", 31270, 32900, 64000, 33000, 30000, 60000, 15000, 6000)

SRGB_PNGINFO = PngInfo()
SRGB_PNGINFO.add(b"sRGB", b'\3')
SRGB_PNGINFO.add(b"gAMA", GAMA_SRGB)
SRGB_PNGINFO.add(b"cHRM", CHRM_SRGB)

image = Image.new('RGB', (640, 480))
with open("Download_srgb5.png", "wb") as f:
    image.save(f, format="png", compress_level=6, pnginfo=SRGB_PNGINFO)

Significance

Chrome 61 expects images to have a color profile embedded in them. In my case, it is of vital importance that the generated images are being shown using the sRGB color profile.

@wiredfool
Copy link
Member

That was added here: https://github.com/python-pillow/Pillow/pull/2347/files

The spec says:

When the sRGB chunk is present, it is recommended that decoders that recognize it and are capable of colour management [ICC] ignore the gAMA and cHRM chunks and use the sRGB chunk instead. Decoders that recognize the sRGB chunk but are not capable of colour management [ICC] are recommended to ignore the gAMA and cHRM chunks, and use the values given above as if they had appeared in gAMA and cHRM chunks.

It is recommended that the sRGB and iCCP chunks do not both appear in a PNG datastream

So, it appears in the spec that the proper set is (iCCP | sRGB) + gAMA + cHRM. It looks like we're getting that exactly backwards.

stuxcrystal added a commit to stuxcrystal/Pillow that referenced this issue Oct 5, 2017
@stuxcrystal stuxcrystal mentioned this issue Oct 5, 2017
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

No branches or pull requests

2 participants