diff --git a/app/jp2/image.py b/app/jp2/image.py index 4be371a..6e84b92 100644 --- a/app/jp2/image.py +++ b/app/jp2/image.py @@ -141,6 +141,25 @@ def _correct_img_orientation(img: Image, img_filename: str = '') -> Image: return img +""" + Checks if an image has transparency by first checking for a transparency property, + second examining the palette for transparent colors, + and third checking for an alpha component on RGBA images. + + https://stackoverflow.com/a/58567453 +""" +def _image_has_transparency(img: Image) -> bool: + if img.info.get("transparency", None) is not None: + return True + if img.mode == "P": + transparent = img.info.get("transparency", -1) + for _, index in img.getcolors(): + if index == transparent: + return True + elif img.mode == "RGBA": + return True + + return False def _convert_img_colour_profile(img: Image, img_filename: str = '') -> Image: """ If the image has ICC profile information, apply a transformation to this @@ -161,8 +180,9 @@ def _convert_img_colour_profile(img: Image, img_filename: str = '') -> Image: img_colour_profile_name, sRGB_profile.profile_description ) + output_mode = 'RGBA' if _image_has_transparency(img) else 'RGB' img = ImageCms.profileToProfile( - img, img_colour_profile, sRGB_profile, outputMode='RGB') + img, img_colour_profile, sRGB_profile, outputMode=output_mode) return img diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e69de29 diff --git a/tests/fixtures/sloth.png b/tests/fixtures/sloth.png new file mode 100644 index 0000000..74d166c Binary files /dev/null and b/tests/fixtures/sloth.png differ diff --git a/tests/test_appetiser.py b/tests/test_appetiser.py index 351fd75..b325201 100644 --- a/tests/test_appetiser.py +++ b/tests/test_appetiser.py @@ -142,3 +142,12 @@ def test_convert_ppm(appetiser_client, fixtures_dir, temp_dir): 'kdu_med', [30, 600, 400, 100] ) + +def test_convert_png(appetiser_client, fixtures_dir, temp_dir): + _convert_test_runner( + appetiser_client, + temp_dir, + fixtures_dir.joinpath('sloth.png'), + 'kdu_med', + [30, 600, 400, 100] + )