Skip to main content

images/test_srgbify.py

1import filecmp
2from pathlib import Path
4from PIL import Image
6from srgbify import convert_image_to_srgb
9def test_it_ignores_an_image_which_is_already_srgb():
10 """
11 If an image is already in sRGB, we don't need to do anything.
13 This image was downloaded from https://webkit.org/blog-files/color-gamut/
14 """
15 im = Image.open("images/examples/Shoes-sRGB.jpg")
17 assert convert_image_to_srgb(im) is None
20def test_it_converts_a_display_p3_image_to_srgb(tmp_path):
21 """
22 If an image is in Display P3, it gets converted to sRGB.
24 This image was downloaded from https://webkit.org/blog-files/color-gamut/
25 """
26 im = Image.open("images/examples/Iceland-P3.jpg")
28 new_im = convert_image_to_srgb(im)
30 new_im.save(tmp_path / "Iceland-sRGB-actual.jpg")
32 assert filecmp.cmp(
33 tmp_path / "Iceland-sRGB-actual.jpg",
34 "images/examples/Iceland-sRGB-expected.jpg",
35 shallow=False,
36 )
39def test_it_preserves_transparency_in_png_images():
40 """
41 If an image is a PNG with transparency, that gets preserved when converting
42 to sRGB.
44 This image is a screenshot from my own computer.
45 """
46 im = Image.open("images/examples/screenshot-with-transparency.png")
48 new_im = convert_image_to_srgb(im)
50 assert new_im.mode == "RGBA"
52 # The top left-hand corner should be transparent
53 assert new_im.getpixel((0, 0)) == (0, 0, 0, 0)
56def test_it_converts_images_with_a_grey_profile():
57 """
58 If an image has a grey colour profile, that gets converted to sRGB.
60 This image is from https://www.flickr.com/photos/lselibrary/3925727501/
61 """
62 im = Image.open("images/examples/3925727501_6aa7c94c10_w.jpg")
64 new_im = convert_image_to_srgb(im)
66 assert new_im.mode == "RGB"
69def test_it_preserves_rotation_from_exif_orientation(tmp_path: Path):
70 """
71 This is based on a photo exported from my Apple Photos Library
72 which was rotated by 90 degrees upon transformation.
74 This was caused by an image with an EXIF orientation tag that was
75 being stripped on save. I found a solution in the Pillow issue tracker.
77 See https://github.com/alexwlchan/scripts/issues/21
78 See https://github.com/python-pillow/Pillow/issues/4703
79 """
80 im = Image.open("images/examples/taylorswift.jpg")
82 new_im = convert_image_to_srgb(im)
84 assert new_im.size == (3024, 4032)
87def test_it_converts_images_with_a_cmyk_profile():
88 """
89 If an image has a CMYK colour profile, that gets converted to sRGB.
90 """
91 im = Image.open("images/examples/the-design-of-everyday-things.jpg")
93 new_im = convert_image_to_srgb(im)
95 assert new_im.mode == "RGB"