Pillow/Tests/test_imageenhance.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.6 KiB
Python
Raw Permalink Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2022-10-03 08:57:42 +03:00
import pytest
from PIL import Image, ImageEnhance
2020-02-12 19:29:19 +03:00
from .helper import assert_image_equal, hopper
def test_sanity() -> None:
2020-02-12 19:29:19 +03:00
# FIXME: assert_image
# Implicit asserts no exception:
ImageEnhance.Color(hopper()).enhance(0.5)
ImageEnhance.Contrast(hopper()).enhance(0.5)
ImageEnhance.Brightness(hopper()).enhance(0.5)
ImageEnhance.Sharpness(hopper()).enhance(0.5)
def test_crash() -> None:
2020-02-12 19:29:19 +03:00
# crashes on small images
im = Image.new("RGB", (1, 1))
ImageEnhance.Sharpness(im).enhance(0.5)
2024-02-17 07:00:38 +03:00
def _half_transparent_image() -> Image.Image:
2020-02-12 19:29:19 +03:00
# returns an image, half transparent, half solid
im = hopper("RGB")
transparent = Image.new("L", im.size, 0)
solid = Image.new("L", (im.size[0] // 2, im.size[1]), 255)
transparent.paste(solid, (0, 0))
im.putalpha(transparent)
return im
2024-02-17 07:00:38 +03:00
def _check_alpha(
im: Image.Image, original: Image.Image, op: str, amount: float
) -> None:
2020-02-12 19:29:19 +03:00
assert im.getbands() == original.getbands()
assert_image_equal(
2020-09-01 20:16:46 +03:00
im.getchannel("A"),
original.getchannel("A"),
f"Diff on {op}: {amount}",
2020-02-12 19:29:19 +03:00
)
2022-10-03 08:57:42 +03:00
@pytest.mark.parametrize("op", ("Color", "Brightness", "Contrast", "Sharpness"))
2024-02-17 07:00:38 +03:00
def test_alpha(op: str) -> None:
2020-02-12 19:29:19 +03:00
# Issue https://github.com/python-pillow/Pillow/issues/899
# Is alpha preserved through image enhancement?
original = _half_transparent_image()
2022-10-03 08:57:42 +03:00
for amount in [0, 0.5, 1.0]:
_check_alpha(
getattr(ImageEnhance, op)(original).enhance(amount),
original,
op,
amount,
)