2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2023-05-02 01:41:18 +03:00
|
|
|
import pytest
|
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
from PIL import Image
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
from .helper import hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
def test_sanity():
|
|
|
|
bbox = hopper().getbbox()
|
|
|
|
assert isinstance(bbox, tuple)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
def test_bbox():
|
2020-02-28 14:33:02 +03:00
|
|
|
def check(im, fill_color):
|
|
|
|
assert im.getbbox() is None
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-28 14:33:02 +03:00
|
|
|
im.paste(fill_color, (10, 25, 90, 75))
|
|
|
|
assert im.getbbox() == (10, 25, 90, 75)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-28 14:33:02 +03:00
|
|
|
im.paste(fill_color, (25, 10, 75, 90))
|
|
|
|
assert im.getbbox() == (10, 10, 90, 90)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-28 14:33:02 +03:00
|
|
|
im.paste(fill_color, (-10, -10, 110, 110))
|
|
|
|
assert im.getbbox() == (0, 0, 100, 100)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-02-28 14:33:02 +03:00
|
|
|
# 8-bit mode
|
|
|
|
im = Image.new("L", (100, 100), 0)
|
|
|
|
check(im, 255)
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2019-11-04 01:18:55 +03:00
|
|
|
# 32-bit mode
|
|
|
|
im = Image.new("RGB", (100, 100), 0)
|
2020-02-28 14:33:02 +03:00
|
|
|
check(im, 255)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2020-02-28 14:33:02 +03:00
|
|
|
for mode in ("RGBA", "RGBa"):
|
|
|
|
for color in ((0, 0, 0, 0), (127, 127, 127, 0), (255, 255, 255, 0)):
|
|
|
|
im = Image.new(mode, (100, 100), color)
|
|
|
|
check(im, (255, 255, 255, 255))
|
2019-11-04 01:18:55 +03:00
|
|
|
|
2020-02-28 14:33:02 +03:00
|
|
|
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"))
|
|
|
|
def test_bbox_alpha_only_false(mode):
|
|
|
|
im = Image.new(mode, (100, 100))
|
2023-06-14 04:29:22 +03:00
|
|
|
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))
|
2023-06-14 04:29:22 +03:00
|
|
|
assert im.getbbox(alpha_only=False) == (25, 25, 75, 75)
|
2023-05-02 01:41:18 +03:00
|
|
|
|
|
|
|
assert im.getbbox() is None
|