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
Prev Previous commit
Next Next commit
Reformat code
  • Loading branch information
andre-fuchs committed Nov 2, 2023
commit fadd8407e8bb00735a0532a66cf35af6398d7e6c
56 changes: 30 additions & 26 deletions tests/test_pillow.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,25 @@ def test_save_as_jpeg_progressive(self):

def test_save_as_jpeg_with_icc_profile(self):
# Testing two different color profiles two cover the standard case and a special one as well
images = [
'colorchecker_sRGB.jpg',
'colorchecker_ECI_RGB_v2.jpg'
]
images = ["colorchecker_sRGB.jpg", "colorchecker_ECI_RGB_v2.jpg"]
for img_name in images:
with open(f'tests/images/{img_name}', 'rb') as f:
with open(f"tests/images/{img_name}", "rb") as f:
image = PillowImage.open(JPEGImageFile(f))
icc_profile = PILImage.open(f).info.get('icc_profile')
icc_profile = PILImage.open(f).info.get("icc_profile")
self.assertIsNotNone(icc_profile)

saved = image.save_as_jpeg(io.BytesIO())
saved_icc_profile = PILImage.open(saved.f).info.get('icc_profile')
saved_icc_profile = PILImage.open(saved.f).info.get("icc_profile")
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:
with open("tests/images/colorchecker_sRGB.jpg", "rb") as f:
image = PillowImage.open(JPEGImageFile(f))
exif = PILImage.open(f).info.get('exif')
exif = PILImage.open(f).info.get("exif")
self.assertIsNotNone(exif)

saved = image.save_as_jpeg(io.BytesIO())
saved_exif = PILImage.open(saved.f).info.get('exif')
saved_exif = PILImage.open(saved.f).info.get("exif")
self.assertEqual(saved_exif, exif)

def test_save_as_png(self):
Expand Down Expand Up @@ -213,18 +210,15 @@ def test_save_mode_cmyk_as_png(self):

def test_save_as_png_with_icc_profile(self):
# Testing two different color profiles two cover the standard case and a special one as well
images = [
'colorchecker_sRGB.jpg',
'colorchecker_ECI_RGB_v2.jpg'
]
images = ["colorchecker_sRGB.jpg", "colorchecker_ECI_RGB_v2.jpg"]
for img_name in images:
with open(f'tests/images/{img_name}', 'rb') as f:
with open(f"tests/images/{img_name}", "rb") as f:
image = PillowImage.open(JPEGImageFile(f))
icc_profile = PILImage.open(f).info.get('icc_profile')
icc_profile = PILImage.open(f).info.get("icc_profile")
self.assertIsNotNone(icc_profile)

saved = image.save_as_png(io.BytesIO())
saved_icc_profile = PILImage.open(saved.f).info.get('icc_profile')
saved_icc_profile = PILImage.open(saved.f).info.get("icc_profile")
self.assertEqual(saved_icc_profile, icc_profile)

def test_save_as_gif(self):
Expand Down Expand Up @@ -346,7 +340,6 @@ def test_open_webp_w_alpha(self):
self.assertTrue(image.has_alpha())
self.assertFalse(image.has_animation())


@unittest.skipIf(no_webp_support, "Pillow does not have WebP support")
def test_save_webp_quality(self):
high_quality = self.image.save_as_webp(io.BytesIO(), quality=90)
Expand All @@ -366,18 +359,15 @@ def test_save_webp_lossless(self):
@unittest.skipIf(no_webp_support, "Pillow does not have WebP support")
def test_save_as_webp_with_icc_profile(self):
# Testing two different color profiles two cover the standard case and a special one as well
images = [
'colorchecker_sRGB.jpg',
'colorchecker_ECI_RGB_v2.jpg'
]
images = ["colorchecker_sRGB.jpg", "colorchecker_ECI_RGB_v2.jpg"]
for img_name in images:
with open(f'tests/images/{img_name}', 'rb') as f:
with open(f"tests/images/{img_name}", "rb") as f:
image = PillowImage.open(JPEGImageFile(f))
icc_profile = PILImage.open(f).info.get('icc_profile')
icc_profile = PILImage.open(f).info.get("icc_profile")
self.assertIsNotNone(icc_profile)

saved = image.save_as_webp(io.BytesIO())
saved_icc_profile = PILImage.open(saved.f).info.get('icc_profile')
saved_icc_profile = PILImage.open(saved.f).info.get("icc_profile")
self.assertEqual(saved_icc_profile, icc_profile)

@unittest.skipIf(no_avif_support, "Pillow does not have AVIF support")
Expand All @@ -390,6 +380,20 @@ def test_save_as_avif(self):
self.assertIsInstance(return_value, AvifImageFile)
self.assertEqual(return_value.f, output)

@unittest.skipIf(no_avif_support, "Pillow does not have AVIF support")
def test_save_as_avif_with_icc_profile(self):
# Testing two different color profiles two cover the standard case and a special one as well
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 = PillowImage.open(JPEGImageFile(f))
icc_profile = PILImage.open(f).info.get("icc_profile")
self.assertIsNotNone(icc_profile)

saved = image.save_as_avif(io.BytesIO())
saved_icc_profile = PILImage.open(saved.f).info.get("icc_profile")
self.assertEqual(saved_icc_profile, icc_profile)

@unittest.skipIf(no_avif_support, "Pillow does not have AVIF support")
def test_open_avif(self):
with open("tests/images/tree.webp", "rb") as f:
Expand Down Expand Up @@ -508,7 +512,7 @@ def assert_orientation_landscape_image_is_correct(self, image):
def assert_exif_orientation_equals_value(self, image, value):
exif = image.image.getexif()
self.assertIsNotNone(exif)
self.assertEqual(exif.get(0x0112, 1), value) # 0x0112 = Orientation
self.assertEqual(exif.get(0x0112, 1), value) # 0x0112 = Orientation

def test_jpeg_with_orientation_1(self):
with open("tests/images/orientation/landscape_1.jpg", "rb") as f:
Expand Down
22 changes: 11 additions & 11 deletions willow/plugins/pillow.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
pass

from PIL import ImageOps

from willow.image import (
AvifImageFile,
BadImageOperationError,
Expand Down Expand Up @@ -172,19 +173,19 @@ def add_icc_profile(self, image, kwargs):
"""
Take over the ICC color profile from the original image
"""
icc_profile = image.info.get('icc_profile')
icc_profile = image.info.get("icc_profile")
if icc_profile is not None:
kwargs['icc_profile'] = icc_profile
kwargs["icc_profile"] = icc_profile
return kwargs

@Image.operation
def add_exif_data(self, image, kwargs):
"""
Take over the EXIF data from the original image
"""
exif = image.info.get('exif')
exif = image.info.get("exif")
if exif is not None:
kwargs['exif'] = exif
kwargs["exif"] = exif
return kwargs

@Image.operation
Expand Down Expand Up @@ -217,7 +218,7 @@ def save_as_jpeg(
kwargs["optimize"] = True
if progressive:
kwargs["progressive"] = True

kwargs = self.add_icc_profile(image, kwargs)
kwargs = self.add_exif_data(image, kwargs)

Expand Down Expand Up @@ -320,28 +321,27 @@ def save_as_heic(
"""
kwargs = {"quality": quality}
if lossless:
kwargs = {"quality": -1, "chroma": 444}
kwargs = {"quality": -1, "chroma": 444}

kwargs = self.add_icc_profile(self.image, kwargs)

self.image.save(f, "HEIF", **kwargs)

if not lossless and apply_optimizers:
self.optimize(f, "heic")

return HeicImageFile(f)

@Image.operation
def save_as_avif(self, f, quality=80, lossless=False, apply_optimizers=True):

kwargs = {"quality": quality}
if lossless:
kwargs = {"quality": -1, "chroma": 444}

kwargs = self.add_icc_profile(self.image, kwargs)

self.image.save(f, "AVIF", **kwargs)

if not lossless and apply_optimizers:
self.optimize(f, "heic")

Expand Down