Pillow/Tests/test_image_paste.py

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

346 lines
10 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2022-08-22 17:23:42 +03:00
import pytest
2016-09-03 17:42:35 +03:00
from PIL import Image
2022-04-10 21:26:23 +03:00
from .helper import CachedProperty, assert_image_equal
2016-09-03 17:42:35 +03:00
2020-03-28 04:51:28 +03:00
class TestImagingPaste:
2016-09-19 02:49:41 +03:00
size = 128
2016-09-03 17:42:35 +03:00
2024-02-07 11:16:28 +03:00
def assert_9points_image(
self, im: Image.Image, expected: list[tuple[int, int, int, int]]
) -> None:
2016-09-19 02:49:41 +03:00
px = im.load()
actual = [
px[0, 0],
px[self.size // 2, 0],
px[self.size - 1, 0],
px[0, self.size // 2],
px[self.size // 2, self.size // 2],
px[self.size - 1, self.size // 2],
px[0, self.size - 1],
px[self.size // 2, self.size - 1],
px[self.size - 1, self.size - 1],
]
2024-02-07 11:16:28 +03:00
assert actual == [
point[0] if im.mode == "L" else point[: len(im.mode)] for point in expected
]
2016-09-19 02:49:41 +03:00
2024-02-07 11:16:28 +03:00
def assert_9points_paste(
self,
im: Image.Image,
2024-03-02 05:12:17 +03:00
im2: Image.Image | str | tuple[int, ...],
2024-02-07 11:16:28 +03:00
mask: Image.Image,
expected: list[tuple[int, int, int, int]],
) -> None:
im3 = im.copy()
im3.paste(im2, (0, 0), mask)
self.assert_9points_image(im3, expected)
# Abbreviated syntax
im.paste(im2, mask)
self.assert_9points_image(im, expected)
2022-04-10 21:26:23 +03:00
@CachedProperty
2024-02-12 01:28:53 +03:00
def mask_1(self) -> Image.Image:
2019-06-13 18:54:24 +03:00
mask = Image.new("1", (self.size, self.size))
2016-09-03 17:42:35 +03:00
px = mask.load()
for y in range(mask.height):
for x in range(mask.width):
px[y, x] = (x + y) % 2
return mask
2022-04-10 21:26:23 +03:00
@CachedProperty
2024-02-12 01:28:53 +03:00
def mask_L(self) -> Image.Image:
2022-01-15 01:02:31 +03:00
return self.gradient_L.transpose(Image.Transpose.ROTATE_270)
2016-09-19 02:49:41 +03:00
2022-04-10 21:26:23 +03:00
@CachedProperty
2024-02-12 01:28:53 +03:00
def gradient_L(self) -> Image.Image:
2019-06-13 18:54:24 +03:00
gradient = Image.new("L", (self.size, self.size))
2016-09-19 02:49:41 +03:00
px = gradient.load()
for y in range(gradient.height):
for x in range(gradient.width):
2016-09-03 17:42:35 +03:00
px[y, x] = (x + y) % 255
2016-09-19 02:49:41 +03:00
return gradient
2016-09-03 17:42:35 +03:00
2022-04-10 21:26:23 +03:00
@CachedProperty
2024-02-12 01:28:53 +03:00
def gradient_RGB(self) -> Image.Image:
2019-06-13 18:54:24 +03:00
return Image.merge(
"RGB",
[
self.gradient_L,
2022-01-15 01:02:31 +03:00
self.gradient_L.transpose(Image.Transpose.ROTATE_90),
self.gradient_L.transpose(Image.Transpose.ROTATE_180),
2019-06-13 18:54:24 +03:00
],
)
2016-09-03 17:42:35 +03:00
2022-04-10 21:26:23 +03:00
@CachedProperty
2024-02-12 01:28:53 +03:00
def gradient_LA(self) -> Image.Image:
return Image.merge(
"LA",
[
self.gradient_L,
self.gradient_L.transpose(Image.Transpose.ROTATE_90),
],
)
2022-04-10 21:26:23 +03:00
@CachedProperty
2024-02-12 01:28:53 +03:00
def gradient_RGBA(self) -> Image.Image:
2019-06-13 18:54:24 +03:00
return Image.merge(
"RGBA",
[
self.gradient_L,
2022-01-15 01:02:31 +03:00
self.gradient_L.transpose(Image.Transpose.ROTATE_90),
self.gradient_L.transpose(Image.Transpose.ROTATE_180),
self.gradient_L.transpose(Image.Transpose.ROTATE_270),
2019-06-13 18:54:24 +03:00
],
)
2016-09-19 02:49:41 +03:00
2022-04-10 21:26:23 +03:00
@CachedProperty
2024-02-12 01:28:53 +03:00
def gradient_RGBa(self) -> Image.Image:
2019-06-13 18:54:24 +03:00
return Image.merge(
"RGBa",
[
self.gradient_L,
2022-01-15 01:02:31 +03:00
self.gradient_L.transpose(Image.Transpose.ROTATE_90),
self.gradient_L.transpose(Image.Transpose.ROTATE_180),
self.gradient_L.transpose(Image.Transpose.ROTATE_270),
2019-06-13 18:54:24 +03:00
],
)
2016-09-03 17:42:35 +03:00
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_image_solid(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), "red")
im2 = getattr(self, "gradient_" + mode)
im.paste(im2, (12, 23))
im = im.crop((12, 23, im2.width + 12, im2.height + 23))
assert_image_equal(im, im2)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_image_mask_1(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), "white")
im2 = getattr(self, "gradient_" + mode)
self.assert_9points_paste(
im,
im2,
self.mask_1,
[
(255, 255, 255, 255),
(255, 255, 255, 255),
(127, 254, 127, 0),
(255, 255, 255, 255),
(255, 255, 255, 255),
(191, 190, 63, 64),
(127, 0, 127, 254),
(191, 64, 63, 190),
(255, 255, 255, 255),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_image_mask_L(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), "white")
im2 = getattr(self, "gradient_" + mode)
self.assert_9points_paste(
im,
im2,
self.mask_L,
[
(128, 191, 255, 191),
(208, 239, 239, 208),
(255, 255, 255, 255),
(112, 111, 206, 207),
(192, 191, 191, 191),
(239, 239, 207, 207),
(128, 1, 128, 254),
(207, 113, 112, 207),
(255, 191, 128, 191),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_image_mask_LA(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), "white")
im2 = getattr(self, "gradient_" + mode)
self.assert_9points_paste(
im,
im2,
self.gradient_LA,
[
(128, 191, 255, 191),
(112, 207, 206, 111),
(128, 254, 128, 1),
(208, 208, 239, 239),
(192, 191, 191, 191),
(207, 207, 112, 113),
(255, 255, 255, 255),
(239, 207, 207, 239),
(255, 191, 128, 191),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_image_mask_RGBA(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), "white")
im2 = getattr(self, "gradient_" + mode)
self.assert_9points_paste(
im,
im2,
self.gradient_RGBA,
[
(128, 191, 255, 191),
(208, 239, 239, 208),
(255, 255, 255, 255),
(112, 111, 206, 207),
(192, 191, 191, 191),
(239, 239, 207, 207),
(128, 1, 128, 254),
(207, 113, 112, 207),
(255, 191, 128, 191),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_image_mask_RGBa(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), "white")
im2 = getattr(self, "gradient_" + mode)
self.assert_9points_paste(
im,
im2,
self.gradient_RGBa,
[
(128, 255, 126, 255),
(0, 127, 126, 255),
(126, 253, 126, 255),
(128, 127, 254, 255),
(0, 255, 254, 255),
(126, 125, 254, 255),
(128, 1, 128, 255),
(0, 129, 128, 255),
(126, 255, 128, 255),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_color_solid(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), "black")
rect = (12, 23, 128 + 12, 128 + 23)
im.paste("white", rect)
hist = im.crop(rect).histogram()
while hist:
head, hist = hist[:256], hist[256:]
assert head[255] == 128 * 128
assert sum(head[:255]) == 0
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_color_mask_1(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = Image.new(mode, (200, 200), (50, 60, 70, 80)[: len(mode)])
color = (10, 20, 30, 40)[: len(mode)]
self.assert_9points_paste(
im,
color,
self.mask_1,
[
(50, 60, 70, 80),
(50, 60, 70, 80),
(10, 20, 30, 40),
(50, 60, 70, 80),
(50, 60, 70, 80),
(10, 20, 30, 40),
(10, 20, 30, 40),
(10, 20, 30, 40),
(50, 60, 70, 80),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_color_mask_L(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = getattr(self, "gradient_" + mode).copy()
color = "white"
self.assert_9points_paste(
im,
color,
self.mask_L,
[
(127, 191, 254, 191),
(111, 207, 206, 110),
(127, 254, 127, 0),
(207, 207, 239, 239),
(191, 191, 190, 191),
(207, 206, 111, 112),
(254, 254, 254, 255),
(239, 206, 206, 238),
(254, 191, 127, 191),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_color_mask_RGBA(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = getattr(self, "gradient_" + mode).copy()
color = "white"
self.assert_9points_paste(
im,
color,
self.gradient_RGBA,
[
(127, 191, 254, 191),
(111, 207, 206, 110),
(127, 254, 127, 0),
(207, 207, 239, 239),
(191, 191, 190, 191),
(207, 206, 111, 112),
(254, 254, 254, 255),
(239, 206, 206, 238),
(254, 191, 127, 191),
],
)
@pytest.mark.parametrize("mode", ["RGBA", "RGB", "L"])
2024-02-07 11:16:28 +03:00
def test_color_mask_RGBa(self, mode: str) -> None:
2022-08-22 17:23:42 +03:00
im = getattr(self, "gradient_" + mode).copy()
color = "white"
self.assert_9points_paste(
im,
color,
self.gradient_RGBa,
[
(255, 63, 126, 63),
(47, 143, 142, 46),
(126, 253, 126, 255),
(15, 15, 47, 47),
(63, 63, 62, 63),
(142, 141, 46, 47),
(255, 255, 255, 0),
(48, 15, 15, 47),
(126, 63, 255, 63),
],
)
2016-11-14 13:12:25 +03:00
def test_different_sizes(self) -> None:
2019-06-13 18:54:24 +03:00
im = Image.new("RGB", (100, 100))
im2 = Image.new("RGB", (50, 50))
2016-11-14 13:16:04 +03:00
im.copy().paste(im2)
2017-04-20 14:14:23 +03:00
im.copy().paste(im2, (0, 0))
2024-06-22 03:09:11 +03:00
def test_incorrect_abbreviated_form(self) -> None:
im = Image.new("L", (1, 1))
with pytest.raises(ValueError):
im.paste(im, im, im)