Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 21 additions & 1 deletion app/jp2/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is intended as a docstring, should be inside the function definition, otherwise treat as a comment.

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
Expand All @@ -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

Expand Down
Empty file added pyproject.toml
Empty file.
Binary file added tests/fixtures/sloth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions tests/test_appetiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)