Pillow/Tests/test_image_getbbox.py

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

58 lines
1.6 KiB
Python
Raw Permalink Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2023-05-02 01:41:18 +03:00
import pytest
from PIL import Image
from .helper import hopper
2024-01-25 14:18:46 +03:00
def test_sanity() -> None:
bbox = hopper().getbbox()
assert isinstance(bbox, tuple)
2024-01-25 14:18:46 +03:00
def test_bbox() -> None:
def check(im: Image.Image, fill_color: int | tuple[int, ...]) -> None:
assert im.getbbox() is None
im.paste(fill_color, (10, 25, 90, 75))
assert im.getbbox() == (10, 25, 90, 75)
im.paste(fill_color, (25, 10, 75, 90))
assert im.getbbox() == (10, 10, 90, 90)
im.paste(fill_color, (-10, -10, 110, 110))
assert im.getbbox() == (0, 0, 100, 100)
# 8-bit mode
im = Image.new("L", (100, 100), 0)
check(im, 255)
# 32-bit mode
im = Image.new("RGB", (100, 100), 0)
check(im, 255)
2014-06-10 13:10:47 +04:00
for mode in ("RGBA", "RGBa"):
2024-01-25 14:18:46 +03:00
for rgba_color in ((0, 0, 0, 0), (127, 127, 127, 0), (255, 255, 255, 0)):
im = Image.new(mode, (100, 100), rgba_color)
check(im, (255, 255, 255, 255))
for mode in ("La", "LA", "PA"):
for color in ((0, 0), (127, 0), (255, 0)):
im = Image.new(mode, (100, 100), color)
check(im, (255, 255))
2023-05-02 01:41:18 +03:00
@pytest.mark.parametrize("mode", ("RGBA", "RGBa", "La", "LA", "PA"))
2024-01-25 14:18:46 +03:00
def test_bbox_alpha_only_false(mode: str) -> None:
2023-05-02 01:41:18 +03:00
im = Image.new(mode, (100, 100))
assert im.getbbox(alpha_only=False) is None
2023-05-02 01:41:18 +03:00
fill_color = [1] * Image.getmodebands(mode)
fill_color[-1] = 0
im.paste(tuple(fill_color), (25, 25, 75, 75))
assert im.getbbox(alpha_only=False) == (25, 25, 75, 75)
2023-05-02 01:41:18 +03:00
assert im.getbbox() is None