2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2020-08-11 17:36:39 +03:00
|
|
|
from PIL import Image, ImageDraw, ImageOps, ImageStat, features
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal,
|
|
|
|
assert_image_similar,
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile,
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_tuple_approx_equal,
|
|
|
|
hopper,
|
|
|
|
)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2024-03-02 05:12:17 +03:00
|
|
|
class Deformer(ImageOps.SupportsGetMesh):
|
|
|
|
def getmesh(
|
|
|
|
self, im: Image.Image
|
|
|
|
) -> list[
|
|
|
|
tuple[tuple[int, int, int, int], tuple[int, int, int, int, int, int, int, int]]
|
|
|
|
]:
|
2020-02-12 19:29:19 +03:00
|
|
|
x, y = im.size
|
|
|
|
return [((0, 0, x, y), (0, 0, x, 0, x, y, y, 0))]
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
deformer = Deformer()
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.autocontrast(hopper("L"))
|
|
|
|
ImageOps.autocontrast(hopper("RGB"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.autocontrast(hopper("L"), cutoff=10)
|
2020-08-06 03:00:13 +03:00
|
|
|
ImageOps.autocontrast(hopper("L"), cutoff=(2, 10))
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.autocontrast(hopper("L"), ignore=[0, 255])
|
2020-08-06 03:00:13 +03:00
|
|
|
ImageOps.autocontrast(hopper("L"), mask=hopper("L"))
|
2021-03-21 21:15:13 +03:00
|
|
|
ImageOps.autocontrast(hopper("L"), preserve_tone=True)
|
2018-09-26 13:07:46 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.colorize(hopper("L"), (0, 0, 0), (255, 255, 255))
|
|
|
|
ImageOps.colorize(hopper("L"), "black", "white")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.pad(hopper("L"), (128, 128))
|
|
|
|
ImageOps.pad(hopper("RGB"), (128, 128))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2021-04-19 13:12:52 +03:00
|
|
|
ImageOps.contain(hopper("L"), (128, 128))
|
|
|
|
ImageOps.contain(hopper("RGB"), (128, 128))
|
|
|
|
|
2023-09-21 04:54:36 +03:00
|
|
|
ImageOps.cover(hopper("L"), (128, 128))
|
|
|
|
ImageOps.cover(hopper("RGB"), (128, 128))
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.crop(hopper("L"), 1)
|
|
|
|
ImageOps.crop(hopper("RGB"), 1)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.deform(hopper("L"), deformer)
|
|
|
|
ImageOps.deform(hopper("RGB"), deformer)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.equalize(hopper("L"))
|
|
|
|
ImageOps.equalize(hopper("RGB"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.expand(hopper("L"), 1)
|
|
|
|
ImageOps.expand(hopper("RGB"), 1)
|
|
|
|
ImageOps.expand(hopper("L"), 2, "blue")
|
|
|
|
ImageOps.expand(hopper("RGB"), 2, "blue")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.fit(hopper("L"), (128, 128))
|
|
|
|
ImageOps.fit(hopper("RGB"), (128, 128))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.flip(hopper("L"))
|
|
|
|
ImageOps.flip(hopper("RGB"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.grayscale(hopper("L"))
|
|
|
|
ImageOps.grayscale(hopper("RGB"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2022-02-08 01:12:01 +03:00
|
|
|
ImageOps.invert(hopper("1"))
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.invert(hopper("L"))
|
|
|
|
ImageOps.invert(hopper("RGB"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.mirror(hopper("L"))
|
|
|
|
ImageOps.mirror(hopper("RGB"))
|
2013-07-01 15:36:46 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.posterize(hopper("L"), 4)
|
|
|
|
ImageOps.posterize(hopper("RGB"), 4)
|
2019-03-04 03:49:39 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.solarize(hopper("L"))
|
|
|
|
ImageOps.solarize(hopper("RGB"))
|
2013-07-01 15:36:46 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageOps.exif_transpose(hopper("L"))
|
|
|
|
ImageOps.exif_transpose(hopper("RGB"))
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_1pxfit() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Division by zero in equalize if image is 1 pixel high
|
|
|
|
newimg = ImageOps.fit(hopper("RGB").resize((1, 1)), (35, 35))
|
|
|
|
assert newimg.size == (35, 35)
|
2019-09-29 07:26:32 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
newimg = ImageOps.fit(hopper("RGB").resize((1, 100)), (35, 35))
|
|
|
|
assert newimg.size == (35, 35)
|
2018-09-26 13:07:46 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
newimg = ImageOps.fit(hopper("RGB").resize((100, 1)), (35, 35))
|
|
|
|
assert newimg.size == (35, 35)
|
2018-09-26 13:07:46 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_fit_same_ratio() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# The ratio for this image is 1000.0 / 755 = 1.3245033112582782
|
|
|
|
# If the ratios are not acknowledged to be the same,
|
|
|
|
# and Pillow attempts to adjust the width to
|
|
|
|
# 1.3245033112582782 * 755 = 1000.0000000000001
|
|
|
|
# then centering this greater width causes a negative x offset when cropping
|
|
|
|
with Image.new("RGB", (1000, 755)) as im:
|
|
|
|
new_im = ImageOps.fit(im, (1000, 755))
|
|
|
|
assert new_im.size == (1000, 755)
|
|
|
|
|
|
|
|
|
2021-05-01 15:04:14 +03:00
|
|
|
@pytest.mark.parametrize("new_size", ((256, 256), (512, 256), (256, 512)))
|
2024-02-17 07:00:38 +03:00
|
|
|
def test_contain(new_size: tuple[int, int]) -> None:
|
2021-04-19 13:12:52 +03:00
|
|
|
im = hopper()
|
|
|
|
new_im = ImageOps.contain(im, new_size)
|
2021-05-01 15:04:14 +03:00
|
|
|
assert new_im.size == (256, 256)
|
2021-04-19 13:12:52 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_contain_round() -> None:
|
2022-08-24 15:32:42 +03:00
|
|
|
im = Image.new("1", (43, 63), 1)
|
|
|
|
new_im = ImageOps.contain(im, (5, 7))
|
|
|
|
assert new_im.width == 5
|
|
|
|
|
|
|
|
im = Image.new("1", (63, 43), 1)
|
|
|
|
new_im = ImageOps.contain(im, (7, 5))
|
|
|
|
assert new_im.height == 5
|
|
|
|
|
|
|
|
|
2023-09-21 04:54:36 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"image_name, expected_size",
|
|
|
|
(
|
|
|
|
("colr_bungee.png", (1024, 256)), # landscape
|
|
|
|
("imagedraw_stroke_multiline.png", (256, 640)), # portrait
|
|
|
|
("hopper.png", (256, 256)), # square
|
|
|
|
),
|
|
|
|
)
|
2024-02-17 07:00:38 +03:00
|
|
|
def test_cover(image_name: str, expected_size: tuple[int, int]) -> None:
|
2023-09-21 04:54:36 +03:00
|
|
|
with Image.open("Tests/images/" + image_name) as im:
|
|
|
|
new_im = ImageOps.cover(im, (256, 256))
|
|
|
|
assert new_im.size == expected_size
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pad() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Same ratio
|
|
|
|
im = hopper()
|
|
|
|
new_size = (im.width * 2, im.height * 2)
|
|
|
|
new_im = ImageOps.pad(im, new_size)
|
|
|
|
assert new_im.size == new_size
|
|
|
|
|
|
|
|
for label, color, new_size in [
|
|
|
|
("h", None, (im.width * 4, im.height * 2)),
|
|
|
|
("v", "#f00", (im.width * 2, im.height * 4)),
|
|
|
|
]:
|
|
|
|
for i, centering in enumerate([(0, 0), (0.5, 0.5), (1, 1)]):
|
|
|
|
new_im = ImageOps.pad(im, new_size, color=color, centering=centering)
|
|
|
|
assert new_im.size == new_size
|
|
|
|
|
2021-02-21 14:22:29 +03:00
|
|
|
assert_image_similar_tofile(
|
|
|
|
new_im, "Tests/images/imageops_pad_" + label + "_" + str(i) + ".jpg", 6
|
|
|
|
)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pad_round() -> None:
|
2022-08-24 15:56:19 +03:00
|
|
|
im = Image.new("1", (1, 1), 1)
|
|
|
|
new_im = ImageOps.pad(im, (4, 1))
|
|
|
|
assert new_im.load()[2, 0] == 1
|
|
|
|
|
|
|
|
new_im = ImageOps.pad(im, (1, 4))
|
|
|
|
assert new_im.load()[0, 2] == 1
|
|
|
|
|
|
|
|
|
2022-09-19 14:39:38 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("P", "PA"))
|
2024-02-17 07:00:38 +03:00
|
|
|
def test_palette(mode: str) -> None:
|
2022-09-19 14:39:38 +03:00
|
|
|
im = hopper(mode)
|
2022-09-19 14:34:29 +03:00
|
|
|
|
|
|
|
# Expand
|
|
|
|
expanded_im = ImageOps.expand(im)
|
|
|
|
assert_image_equal(im.convert("RGB"), expanded_im.convert("RGB"))
|
|
|
|
|
|
|
|
# Pad
|
|
|
|
padded_im = ImageOps.pad(im, (256, 128), centering=(0, 0))
|
|
|
|
assert_image_equal(
|
|
|
|
im.convert("RGB"), padded_im.convert("RGB").crop((0, 0, 128, 128))
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pil163() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Division by zero in equalize if < 255 pixels in image (@PIL163)
|
|
|
|
|
|
|
|
i = hopper("RGB").resize((15, 16))
|
|
|
|
|
|
|
|
ImageOps.equalize(i.convert("L"))
|
|
|
|
ImageOps.equalize(i.convert("P"))
|
|
|
|
ImageOps.equalize(i.convert("RGB"))
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_scale() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test the scaling function
|
|
|
|
i = hopper("L").resize((50, 50))
|
|
|
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ImageOps.scale(i, -1)
|
|
|
|
|
|
|
|
newimg = ImageOps.scale(i, 1)
|
|
|
|
assert newimg.size == (50, 50)
|
|
|
|
|
|
|
|
newimg = ImageOps.scale(i, 2)
|
|
|
|
assert newimg.size == (100, 100)
|
|
|
|
|
|
|
|
newimg = ImageOps.scale(i, 0.5)
|
|
|
|
assert newimg.size == (25, 25)
|
|
|
|
|
|
|
|
|
2021-07-16 13:58:32 +03:00
|
|
|
@pytest.mark.parametrize("border", (10, (1, 2, 3, 4)))
|
2024-02-17 07:00:38 +03:00
|
|
|
def test_expand_palette(border: int | tuple[int, int, int, int]) -> None:
|
2021-07-16 13:58:32 +03:00
|
|
|
with Image.open("Tests/images/p_16.tga") as im:
|
|
|
|
im_expanded = ImageOps.expand(im, border, (255, 0, 0))
|
|
|
|
|
|
|
|
if isinstance(border, int):
|
|
|
|
left = top = right = bottom = border
|
|
|
|
else:
|
|
|
|
left, top, right, bottom = border
|
|
|
|
px = im_expanded.convert("RGB").load()
|
2021-06-29 14:24:41 +03:00
|
|
|
for x in range(im_expanded.width):
|
2021-07-16 13:58:32 +03:00
|
|
|
for b in range(top):
|
|
|
|
assert px[x, b] == (255, 0, 0)
|
|
|
|
for b in range(bottom):
|
|
|
|
assert px[x, im_expanded.height - 1 - b] == (255, 0, 0)
|
2021-06-29 14:24:41 +03:00
|
|
|
for y in range(im_expanded.height):
|
2021-07-16 13:58:32 +03:00
|
|
|
for b in range(left):
|
|
|
|
assert px[b, y] == (255, 0, 0)
|
|
|
|
for b in range(right):
|
|
|
|
assert px[im_expanded.width - 1 - b, y] == (255, 0, 0)
|
|
|
|
|
|
|
|
im_cropped = im_expanded.crop(
|
|
|
|
(left, top, im_expanded.width - right, im_expanded.height - bottom)
|
|
|
|
)
|
|
|
|
assert_image_equal(im_cropped, im)
|
2021-06-19 14:24:58 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_colorize_2color() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test the colorizing function with 2-color functionality
|
|
|
|
|
|
|
|
# Open test image (256px by 10px, black to white)
|
|
|
|
with Image.open("Tests/images/bw_gradient.png") as im:
|
|
|
|
im = im.convert("L")
|
|
|
|
|
|
|
|
# Create image with original 2-color functionality
|
|
|
|
im_test = ImageOps.colorize(im, "red", "green")
|
|
|
|
|
|
|
|
# Test output image (2-color)
|
|
|
|
left = (0, 1)
|
|
|
|
middle = (127, 1)
|
|
|
|
right = (255, 1)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(left),
|
|
|
|
(255, 0, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="black test pixel incorrect",
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(middle),
|
|
|
|
(127, 63, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="mid test pixel incorrect",
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(right),
|
|
|
|
(0, 127, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="white test pixel incorrect",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_colorize_2color_offset() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test the colorizing function with 2-color functionality and offset
|
|
|
|
|
|
|
|
# Open test image (256px by 10px, black to white)
|
|
|
|
with Image.open("Tests/images/bw_gradient.png") as im:
|
|
|
|
im = im.convert("L")
|
|
|
|
|
|
|
|
# Create image with original 2-color functionality with offsets
|
|
|
|
im_test = ImageOps.colorize(
|
|
|
|
im, black="red", white="green", blackpoint=50, whitepoint=100
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test output image (2-color) with offsets
|
|
|
|
left = (25, 1)
|
|
|
|
middle = (75, 1)
|
|
|
|
right = (125, 1)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(left),
|
|
|
|
(255, 0, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="black test pixel incorrect",
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(middle),
|
|
|
|
(127, 63, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="mid test pixel incorrect",
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(right),
|
|
|
|
(0, 127, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="white test pixel incorrect",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_colorize_3color_offset() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test the colorizing function with 3-color functionality and offset
|
|
|
|
|
|
|
|
# Open test image (256px by 10px, black to white)
|
|
|
|
with Image.open("Tests/images/bw_gradient.png") as im:
|
|
|
|
im = im.convert("L")
|
|
|
|
|
|
|
|
# Create image with new three color functionality with offsets
|
|
|
|
im_test = ImageOps.colorize(
|
|
|
|
im,
|
|
|
|
black="red",
|
|
|
|
white="green",
|
|
|
|
mid="blue",
|
|
|
|
blackpoint=50,
|
|
|
|
whitepoint=200,
|
|
|
|
midpoint=100,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test output image (3-color) with offsets
|
|
|
|
left = (25, 1)
|
|
|
|
left_middle = (75, 1)
|
|
|
|
middle = (100, 1)
|
|
|
|
right_middle = (150, 1)
|
|
|
|
right = (225, 1)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(left),
|
|
|
|
(255, 0, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="black test pixel incorrect",
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(left_middle),
|
|
|
|
(127, 0, 127),
|
|
|
|
threshold=1,
|
|
|
|
msg="low-mid test pixel incorrect",
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(middle), (0, 0, 255), threshold=1, msg="mid incorrect"
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(right_middle),
|
|
|
|
(0, 63, 127),
|
|
|
|
threshold=1,
|
|
|
|
msg="high-mid test pixel incorrect",
|
|
|
|
)
|
|
|
|
assert_tuple_approx_equal(
|
|
|
|
im_test.getpixel(right),
|
|
|
|
(0, 127, 0),
|
|
|
|
threshold=1,
|
|
|
|
msg="white test pixel incorrect",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_transpose() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
exts = [".jpg"]
|
2020-02-18 01:03:32 +03:00
|
|
|
if features.check("webp") and features.check("webp_anim"):
|
2020-02-12 19:29:19 +03:00
|
|
|
exts.append(".webp")
|
|
|
|
for ext in exts:
|
|
|
|
with Image.open("Tests/images/hopper" + ext) as base_im:
|
|
|
|
|
2024-02-17 07:00:38 +03:00
|
|
|
def check(orientation_im: Image.Image) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
for im in [
|
|
|
|
orientation_im,
|
|
|
|
orientation_im.copy(),
|
|
|
|
]: # ImageFile # Image
|
|
|
|
if orientation_im is base_im:
|
|
|
|
assert "exif" not in im.info
|
|
|
|
else:
|
|
|
|
original_exif = im.info["exif"]
|
|
|
|
transposed_im = ImageOps.exif_transpose(im)
|
2024-03-02 05:12:17 +03:00
|
|
|
assert transposed_im is not None
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_similar(base_im, transposed_im, 17)
|
|
|
|
if orientation_im is base_im:
|
|
|
|
assert "exif" not in im.info
|
|
|
|
else:
|
|
|
|
assert transposed_im.info["exif"] != original_exif
|
|
|
|
|
2021-06-18 12:01:12 +03:00
|
|
|
assert 0x0112 in im.getexif()
|
2020-02-12 19:29:19 +03:00
|
|
|
assert 0x0112 not in transposed_im.getexif()
|
|
|
|
|
|
|
|
# Repeat the operation to test that it does not keep transposing
|
|
|
|
transposed_im2 = ImageOps.exif_transpose(transposed_im)
|
2024-03-02 05:12:17 +03:00
|
|
|
assert transposed_im2 is not None
|
2020-02-12 19:29:19 +03:00
|
|
|
assert_image_equal(transposed_im2, transposed_im)
|
|
|
|
|
|
|
|
check(base_im)
|
|
|
|
for i in range(2, 9):
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open(
|
2020-02-12 19:29:19 +03:00
|
|
|
"Tests/images/hopper_orientation_" + str(i) + ext
|
|
|
|
) as orientation_im:
|
|
|
|
check(orientation_im)
|
2020-07-01 02:01:06 +03:00
|
|
|
|
2021-07-06 03:37:28 +03:00
|
|
|
# Orientation from "XML:com.adobe.xmp" info key
|
2022-07-27 15:18:39 +03:00
|
|
|
for suffix in ("", "_exiftool"):
|
|
|
|
with Image.open("Tests/images/xmp_tags_orientation" + suffix + ".png") as im:
|
|
|
|
assert im.getexif()[0x0112] == 3
|
2021-07-06 03:37:28 +03:00
|
|
|
|
2022-07-27 15:18:39 +03:00
|
|
|
transposed_im = ImageOps.exif_transpose(im)
|
2024-03-02 05:12:17 +03:00
|
|
|
assert transposed_im is not None
|
2022-07-27 15:18:39 +03:00
|
|
|
assert 0x0112 not in transposed_im.getexif()
|
2022-07-26 00:30:32 +03:00
|
|
|
|
2022-07-27 15:27:43 +03:00
|
|
|
transposed_im._reload_exif()
|
|
|
|
assert 0x0112 not in transposed_im.getexif()
|
|
|
|
|
2021-07-06 03:37:28 +03:00
|
|
|
# Orientation from "Raw profile type exif" info key
|
|
|
|
# This test image has been manually hexedited from exif_imagemagick.png
|
|
|
|
# to have a different orientation
|
|
|
|
with Image.open("Tests/images/exif_imagemagick_orientation.png") as im:
|
|
|
|
assert im.getexif()[0x0112] == 3
|
|
|
|
|
|
|
|
transposed_im = ImageOps.exif_transpose(im)
|
2024-03-02 05:12:17 +03:00
|
|
|
assert transposed_im is not None
|
2021-07-06 03:37:28 +03:00
|
|
|
assert 0x0112 not in transposed_im.getexif()
|
|
|
|
|
|
|
|
# Orientation set directly on Image.Exif
|
|
|
|
im = hopper()
|
|
|
|
im.getexif()[0x0112] = 3
|
|
|
|
transposed_im = ImageOps.exif_transpose(im)
|
2024-03-02 05:12:17 +03:00
|
|
|
assert transposed_im is not None
|
2021-07-06 03:37:28 +03:00
|
|
|
assert 0x0112 not in transposed_im.getexif()
|
|
|
|
|
2020-07-01 02:37:17 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_exif_transpose_in_place() -> None:
|
2023-04-15 14:03:59 +03:00
|
|
|
with Image.open("Tests/images/orientation_rectangle.jpg") as im:
|
|
|
|
assert im.size == (2, 1)
|
|
|
|
assert im.getexif()[0x0112] == 8
|
|
|
|
expected = im.rotate(90, expand=True)
|
|
|
|
|
2023-06-14 09:12:47 +03:00
|
|
|
ImageOps.exif_transpose(im, in_place=True)
|
2023-04-15 14:03:59 +03:00
|
|
|
assert im.size == (1, 2)
|
|
|
|
assert 0x0112 not in im.getexif()
|
|
|
|
assert_image_equal(im, expected)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_autocontrast_unsupported_mode() -> None:
|
2023-10-24 04:44:00 +03:00
|
|
|
im = Image.new("RGBA", (1, 1))
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
ImageOps.autocontrast(im)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_autocontrast_cutoff() -> None:
|
2020-07-01 02:01:06 +03:00
|
|
|
# Test the cutoff argument of autocontrast
|
|
|
|
with Image.open("Tests/images/bw_gradient.png") as img:
|
2020-07-02 12:01:56 +03:00
|
|
|
|
2024-06-09 08:16:17 +03:00
|
|
|
def autocontrast(cutoff: int | tuple[int, int]) -> list[int]:
|
2020-07-02 12:01:56 +03:00
|
|
|
return ImageOps.autocontrast(img, cutoff).histogram()
|
|
|
|
|
|
|
|
assert autocontrast(10) == autocontrast((10, 10))
|
|
|
|
assert autocontrast(10) != autocontrast((1, 10))
|
2020-08-06 03:00:13 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_autocontrast_mask_toy_input() -> None:
|
2020-08-06 03:00:13 +03:00
|
|
|
# Test the mask argument of autocontrast
|
|
|
|
with Image.open("Tests/images/bw_gradient.png") as img:
|
|
|
|
rect_mask = Image.new("L", img.size, 0)
|
|
|
|
draw = ImageDraw.Draw(rect_mask)
|
2020-08-08 13:32:52 +03:00
|
|
|
x0 = img.size[0] // 4
|
|
|
|
y0 = img.size[1] // 4
|
|
|
|
x1 = 3 * img.size[0] // 4
|
|
|
|
y1 = 3 * img.size[1] // 4
|
2020-08-06 03:00:13 +03:00
|
|
|
draw.rectangle((x0, y0, x1, y1), fill=255)
|
|
|
|
|
2020-08-11 17:36:39 +03:00
|
|
|
result = ImageOps.autocontrast(img, mask=rect_mask)
|
|
|
|
result_nomask = ImageOps.autocontrast(img)
|
2020-08-06 03:00:13 +03:00
|
|
|
|
2020-08-11 17:36:39 +03:00
|
|
|
assert result != result_nomask
|
|
|
|
assert ImageStat.Stat(result, mask=rect_mask).median == [127]
|
|
|
|
assert ImageStat.Stat(result_nomask).median == [128]
|
2020-08-07 08:31:17 +03:00
|
|
|
|
2020-08-11 17:47:48 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_autocontrast_mask_real_input() -> None:
|
2020-08-06 03:00:13 +03:00
|
|
|
# Test the autocontrast with a rectangular mask
|
|
|
|
with Image.open("Tests/images/iptc.jpg") as img:
|
|
|
|
rect_mask = Image.new("L", img.size, 0)
|
|
|
|
draw = ImageDraw.Draw(rect_mask)
|
2020-08-07 08:31:17 +03:00
|
|
|
x0, y0 = img.size[0] // 2, img.size[1] // 2
|
|
|
|
x1, y1 = img.size[0] - 40, img.size[1]
|
2020-08-06 03:00:13 +03:00
|
|
|
draw.rectangle((x0, y0, x1, y1), fill=255)
|
|
|
|
|
|
|
|
result = ImageOps.autocontrast(img, mask=rect_mask)
|
|
|
|
result_nomask = ImageOps.autocontrast(img)
|
|
|
|
|
|
|
|
assert result_nomask != result
|
2020-08-11 18:06:16 +03:00
|
|
|
assert_tuple_approx_equal(
|
2020-08-11 18:08:29 +03:00
|
|
|
ImageStat.Stat(result, mask=rect_mask).median,
|
2024-02-17 07:00:38 +03:00
|
|
|
(195, 202, 184),
|
2020-08-11 18:08:29 +03:00
|
|
|
threshold=2,
|
|
|
|
msg="autocontrast with mask pixel incorrect",
|
|
|
|
)
|
2020-08-11 18:06:16 +03:00
|
|
|
assert_tuple_approx_equal(
|
2020-08-11 18:08:29 +03:00
|
|
|
ImageStat.Stat(result_nomask).median,
|
2024-02-17 07:00:38 +03:00
|
|
|
(119, 106, 79),
|
2020-08-11 18:08:29 +03:00
|
|
|
threshold=2,
|
|
|
|
msg="autocontrast without mask pixel incorrect",
|
|
|
|
)
|
2021-03-22 12:06:44 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_autocontrast_preserve_tone() -> None:
|
2024-03-02 05:12:17 +03:00
|
|
|
def autocontrast(mode: str, preserve_tone: bool) -> list[int]:
|
2021-03-29 15:26:34 +03:00
|
|
|
im = hopper(mode)
|
|
|
|
return ImageOps.autocontrast(im, preserve_tone=preserve_tone).histogram()
|
|
|
|
|
|
|
|
assert autocontrast("RGB", True) != autocontrast("RGB", False)
|
|
|
|
assert autocontrast("L", True) == autocontrast("L", False)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_autocontrast_preserve_gradient() -> None:
|
2021-03-22 15:30:43 +03:00
|
|
|
gradient = Image.linear_gradient("L")
|
2021-03-22 12:06:44 +03:00
|
|
|
|
|
|
|
# test with a grayscale gradient that extends to 0,255.
|
|
|
|
# Should be a noop.
|
|
|
|
out = ImageOps.autocontrast(gradient, cutoff=0, preserve_tone=True)
|
|
|
|
|
|
|
|
assert_image_equal(gradient, out)
|
|
|
|
|
|
|
|
# cutoff the top and bottom
|
2021-03-28 15:02:52 +03:00
|
|
|
# autocontrast should make the first and last histogram entries equal
|
2021-03-25 14:56:30 +03:00
|
|
|
# and, with rounding, should be 10% of the image pixels
|
2021-03-22 12:06:44 +03:00
|
|
|
out = ImageOps.autocontrast(gradient, cutoff=10, preserve_tone=True)
|
|
|
|
hist = out.histogram()
|
|
|
|
assert hist[0] == hist[-1]
|
|
|
|
assert hist[-1] == 256 * round(256 * 0.10)
|
|
|
|
|
|
|
|
# in rgb
|
|
|
|
img = gradient.convert("RGB")
|
|
|
|
out = ImageOps.autocontrast(img, cutoff=0, preserve_tone=True)
|
|
|
|
assert_image_equal(img, out)
|
|
|
|
|
|
|
|
|
2021-03-23 12:08:18 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"color", ((255, 255, 255), (127, 255, 0), (127, 127, 127), (0, 0, 0))
|
|
|
|
)
|
2024-02-17 07:00:38 +03:00
|
|
|
def test_autocontrast_preserve_one_color(color: tuple[int, int, int]) -> None:
|
2021-03-23 12:08:18 +03:00
|
|
|
img = Image.new("RGB", (10, 10), color)
|
|
|
|
|
|
|
|
# single color images shouldn't change
|
|
|
|
out = ImageOps.autocontrast(img, cutoff=0, preserve_tone=True)
|
|
|
|
assert_image_equal(img, out) # single color, no cutoff
|
|
|
|
|
|
|
|
# even if there is a cutoff
|
|
|
|
out = ImageOps.autocontrast(
|
2021-03-23 13:09:51 +03:00
|
|
|
img, cutoff=10, preserve_tone=True
|
2021-03-23 12:08:18 +03:00
|
|
|
) # single color 10 cutoff
|
|
|
|
assert_image_equal(img, out)
|