Pillow/Tests/test_imageops_usm.py

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

115 lines
3.2 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2024-02-17 07:00:38 +03:00
from typing import Generator
import pytest
2024-05-30 05:00:50 +03:00
from PIL import Image, ImageFile, ImageFilter
2014-06-10 13:10:47 +04:00
2020-03-27 11:50:51 +03:00
@pytest.fixture
2024-05-30 05:00:50 +03:00
def test_images() -> Generator[dict[str, ImageFile.ImageFile], None, None]:
2020-03-27 11:50:51 +03:00
ims = {
"im": Image.open("Tests/images/hopper.ppm"),
"snakes": Image.open("Tests/images/color_snakes.png"),
}
try:
yield ims
finally:
for im in ims.values():
im.close()
2024-05-30 05:00:50 +03:00
def test_filter_api(test_images: dict[str, ImageFile.ImageFile]) -> None:
2020-03-27 11:50:51 +03:00
im = test_images["im"]
test_filter = ImageFilter.GaussianBlur(2.0)
i = im.filter(test_filter)
assert i.mode == "RGB"
assert i.size == (128, 128)
2024-03-02 05:12:17 +03:00
test_filter2 = ImageFilter.UnsharpMask(2.0, 125, 8)
i = im.filter(test_filter2)
2020-03-27 11:50:51 +03:00
assert i.mode == "RGB"
assert i.size == (128, 128)
2024-05-30 05:00:50 +03:00
def test_usm_formats(test_images: dict[str, ImageFile.ImageFile]) -> None:
2020-03-27 11:50:51 +03:00
im = test_images["im"]
usm = ImageFilter.UnsharpMask
with pytest.raises(ValueError):
im.convert("1").filter(usm)
im.convert("L").filter(usm)
with pytest.raises(ValueError):
im.convert("I").filter(usm)
with pytest.raises(ValueError):
im.convert("F").filter(usm)
im.convert("RGB").filter(usm)
im.convert("RGBA").filter(usm)
im.convert("CMYK").filter(usm)
with pytest.raises(ValueError):
im.convert("YCbCr").filter(usm)
2024-05-30 05:00:50 +03:00
def test_blur_formats(test_images: dict[str, ImageFile.ImageFile]) -> None:
2020-03-27 11:50:51 +03:00
im = test_images["im"]
blur = ImageFilter.GaussianBlur
with pytest.raises(ValueError):
im.convert("1").filter(blur)
with pytest.raises(ValueError):
im.convert("I").filter(blur)
with pytest.raises(ValueError):
im.convert("F").filter(blur)
im.convert("RGB").filter(blur)
im.convert("RGBA").filter(blur)
im.convert("CMYK").filter(blur)
with pytest.raises(ValueError):
im.convert("YCbCr").filter(blur)
2024-05-30 05:00:50 +03:00
def test_usm_accuracy(test_images: dict[str, ImageFile.ImageFile]) -> None:
2020-03-27 11:50:51 +03:00
snakes = test_images["snakes"]
src = snakes.convert("RGB")
i = src.filter(ImageFilter.UnsharpMask(5, 1024, 0))
# Image should not be changed because it have only 0 and 255 levels.
assert i.tobytes() == src.tobytes()
2024-05-30 05:00:50 +03:00
def test_blur_accuracy(test_images: dict[str, ImageFile.ImageFile]) -> None:
2020-03-27 11:50:51 +03:00
snakes = test_images["snakes"]
i = snakes.filter(ImageFilter.GaussianBlur(0.4))
# These pixels surrounded with pixels with 255 intensity.
# They must be very close to 255.
for x, y, c in [
(1, 0, 1),
(2, 0, 1),
(7, 8, 1),
(8, 8, 1),
(2, 9, 1),
(7, 3, 0),
(8, 3, 0),
(5, 8, 0),
(5, 9, 0),
(1, 3, 0),
(4, 3, 2),
(4, 2, 2),
]:
assert i.im.getpixel((x, y))[c] >= 250
# Fuzzy match.
2024-06-24 14:04:33 +03:00
def gp(x: int, y: int) -> tuple[int, ...]:
2020-03-27 11:50:51 +03:00
return i.im.getpixel((x, y))
assert 236 <= gp(7, 4)[0] <= 239
assert 236 <= gp(7, 5)[2] <= 239
assert 236 <= gp(7, 6)[2] <= 239
assert 236 <= gp(7, 7)[1] <= 239
assert 236 <= gp(8, 4)[0] <= 239
assert 236 <= gp(8, 5)[2] <= 239
assert 236 <= gp(8, 6)[2] <= 239
assert 236 <= gp(8, 7)[1] <= 239