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

Fix color management by keeping ICC color profiles and EXIF data in addition #136

Merged
merged 15 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions tests/test_wand.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,38 @@ def test_save_as_jpeg_progressive(self):

self.assertTrue(PILImage.open(image.f).info["progressive"])

def test_save_as_jpeg_with_icc_profile(self):
images = ["colorchecker_sRGB.jpg", "colorchecker_ECI_RGB_v2.jpg"]
for img_name in images:
with open(f"tests/images/{img_name}", "rb") as f:
image = WandImage.open(JPEGImageFile(f))

icc_profile = image.get_wand_image().profiles["ICC"]
self.assertIsNotNone(icc_profile)

buffer = io.BytesIO()
image.save_as_jpeg(buffer)
buffer.seek(0)

saved = WandImage.open(JPEGImageFile(buffer))
saved_icc_profile = saved.get_wand_image().profiles["ICC"]
self.assertEqual(saved_icc_profile, icc_profile)

def test_save_as_jpeg_with_exif(self):
with open("tests/images/colorchecker_sRGB.jpg", "rb") as f:
image = WandImage.open(JPEGImageFile(f))

exif_datetime = image.get_wand_image().metadata.get("exif:DateTime")
self.assertIsNotNone(exif_datetime)

buffer = io.BytesIO()
image.save_as_jpeg(buffer)
buffer.seek(0)

saved = WandImage.open(JPEGImageFile(buffer))
saved_exif_datetime = saved.get_wand_image().metadata.get("exif:DateTime")
self.assertEqual(saved_exif_datetime, exif_datetime)

def test_save_as_png(self):
output = io.BytesIO()
return_value = self.image.save_as_png(output)
Expand Down Expand Up @@ -358,6 +390,24 @@ def test_save_webp_lossless(self):
break
self.assertTrue(identical)

@unittest.skipIf(no_webp_support, "ImageMagick was built without WebP support")
def test_save_as_webp_with_icc_profile(self):
images = ["colorchecker_sRGB.jpg", "colorchecker_ECI_RGB_v2.jpg"]
for img_name in images:
with open(f"tests/images/{img_name}", "rb") as f:
image = WandImage.open(JPEGImageFile(f))

icc_profile = image.get_wand_image().profiles["ICC"]
self.assertIsNotNone(icc_profile)

buffer = io.BytesIO()
image.save_as_webp(buffer)
buffer.seek(0)

saved = WandImage.open(WebPImageFile(buffer))
saved_icc_profile = saved.get_wand_image().profiles["ICC"]
self.assertEqual(saved_icc_profile, icc_profile)


class TestWandImageWithOptimizers(unittest.TestCase):
def setUp(self):
Expand Down
28 changes: 26 additions & 2 deletions willow/plugins/wand.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def set_background_color_rgb(self, color):

return clone

def get_icc_profile(self):
return self.image.metadata.get("icc-profile")

def get_exif_data(self):
return self.image.metadata.get("exif")

@Image.operation
def save_as_jpeg(
self,
Expand All @@ -164,8 +170,18 @@ def save_as_jpeg(
:param apply_optimizers: controls whether to run any configured optimizer libraries
:return: JPEGImageFile
"""
with self.image.convert("pjpeg" if progressive else "jpeg") as converted:
with self.image.clone() as converted:
zerolab marked this conversation as resolved.
Show resolved Hide resolved
converted.format = "pjpeg" if progressive else "jpeg"
converted.compression_quality = quality

icc_profile = self.get_icc_profile()
if icc_profile is not None:
converted.profiles["icc"] = icc_profile

exif_data = self.get_exif_data()
if exif_data is not None:
converted.profiles["exif"] = exif_data

converted.save(file=f)

if apply_optimizers:
Expand Down Expand Up @@ -215,7 +231,9 @@ def save_as_webp(
Note that when lossless=True, this will be ignored.
:return: WebPImageFile
"""
with self.image.convert("webp") as converted:
with self.image.clone() as converted:
converted.format = "webp"

if lossless:
library = _wand_api().library
library.MagickSetOption.argtypes = [c_void_p, c_char_p, c_char_p]
Expand All @@ -226,7 +244,13 @@ def save_as_webp(
)
else:
converted.compression_quality = quality

icc_profile = self.get_icc_profile()
if icc_profile is not None:
converted.profiles["icc"] = icc_profile

converted.save(file=f)

if not lossless and apply_optimizers:
self.optimize(f, "webp")
return WebPImageFile(f)
Expand Down