Skip to content

Commit

Permalink
refactor(imagetools): omit sRGB color profile from thumbnails
Browse files Browse the repository at this point in the history
After some experimentation, it turns out that the old imagemagick code
just discarded any embedded color profile from the source image.

After this commit, we attempt to transform the source image from any
embedded color profile to sRGB, but still omit the sRGB color profile
from the resulting thumbnail.
  • Loading branch information
dairiki committed Apr 15, 2023
1 parent 13d65e2 commit 6bc86e7
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
7 changes: 3 additions & 4 deletions lektor/imagetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ def _convert_color_profile_to_srgb(im: PIL.Image.Image) -> None:
The image is modified **in place**.
If the original image has no embedded color profile is it set to sRGB.
After conversion, any embedded color profile is removed. (The default color
space for the web is "sRGB", so we don't need to embed it.)
"""
# XXX: The old imagemagick code (which ran `convert` with `-strip -colorspace sRGB`)
# did not attempt any colorspace conversion. It simply stripped and ignored any
Expand All @@ -573,7 +574,6 @@ def _convert_color_profile_to_srgb(im: PIL.Image.Image) -> None:
#
# Here we attempt to convert from any embedded colorspace in the source image
# to sRGB.
#
if "icc_profile" in im.info:
profile = PIL.ImageCms.getOpenProfile(io.BytesIO(im.info["icc_profile"]))
profile_name = PIL.ImageCms.getProfileName(profile)
Expand All @@ -582,8 +582,7 @@ def _convert_color_profile_to_srgb(im: PIL.Image.Image) -> None:
# (See https://ninedegreesbelow.com/photography/srgb-profile-comparison.html)
if profile_name.strip() not in ("sRGB", "sRGB IEC61966-2.1", "sRGB built-in"):
PIL.ImageCms.profileToProfile(im, profile, SRGB_PROFILE, inPlace=True)
else:
im.info["icc_profile"] = SRGB_PROFILE_BYTES
im.info.pop("icc_profile")


def _compute_thumbnail(
Expand Down
8 changes: 3 additions & 5 deletions tests/test_imagetools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import io
import os
import re
from pathlib import Path
from unittest import mock

Expand Down Expand Up @@ -106,15 +105,14 @@ def test_convert_color_profile_to_srgb():
_convert_color_profile_to_srgb(im)
# Top center of image is red after color transform
assert im.getpixel((im.width // 2, 0)) == approx((255, 0, 0), abs=10)
assert "icc_profile" not in im.info


@pytest.mark.requirespillow
def test_convert_color_profile_to_srgb_add_profile():
def test_convert_color_profile_to_srgb_no_profile():
im = PIL.Image.new("RGB", (100, 100), "#999")
_convert_color_profile_to_srgb(im)
profile = PIL.ImageCms.getOpenProfile(io.BytesIO(im.info["icc_profile"]))
profile_name = PIL.ImageCms.getProfileName(profile)
assert re.match(r"sRGB\b", profile_name)
assert "icc_profile" not in im.info


@pytest.mark.requirespillow
Expand Down

0 comments on commit 6bc86e7

Please sign in to comment.