2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2023-08-26 10:00:04 +03:00
|
|
|
import contextlib
|
2014-04-04 12:27:08 +04:00
|
|
|
import os.path
|
2024-05-27 14:59:32 +03:00
|
|
|
from typing import Sequence
|
2014-04-04 12:27:08 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
import pytest
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2023-08-26 10:00:04 +03:00
|
|
|
from PIL import Image, ImageColor, ImageDraw, ImageFont, features
|
2024-02-05 23:49:43 +03:00
|
|
|
from PIL._typing import Coords
|
2015-08-25 15:27:18 +03:00
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
from .helper import (
|
|
|
|
assert_image_equal,
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile,
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile,
|
2020-02-18 01:03:32 +03:00
|
|
|
hopper,
|
|
|
|
skip_unless_feature,
|
|
|
|
)
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2014-04-04 12:27:08 +04:00
|
|
|
BLACK = (0, 0, 0)
|
|
|
|
WHITE = (255, 255, 255)
|
|
|
|
GRAY = (190, 190, 190)
|
2019-06-13 18:54:46 +03:00
|
|
|
DEFAULT_MODE = "RGB"
|
|
|
|
IMAGES_PATH = os.path.join("Tests", "images", "imagedraw")
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2014-05-12 18:32:04 +04:00
|
|
|
# Image size
|
2014-06-23 23:51:31 +04:00
|
|
|
W, H = 100, 100
|
2014-05-12 18:32:04 +04:00
|
|
|
|
|
|
|
# Bounding box points
|
2014-06-23 23:51:31 +04:00
|
|
|
X0 = int(W / 4)
|
|
|
|
X1 = int(X0 * 3)
|
|
|
|
Y0 = int(H / 4)
|
|
|
|
Y1 = int(X0 * 3)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
# Bounding boxes
|
|
|
|
BBOX = (((X0, Y0), (X1, Y1)), [(X0, Y0), (X1, Y1)], (X0, Y0, X1, Y1), [X0, Y0, X1, Y1])
|
|
|
|
|
|
|
|
# Coordinate sequences
|
|
|
|
POINTS = (
|
|
|
|
((10, 10), (20, 40), (30, 30)),
|
|
|
|
[(10, 10), (20, 40), (30, 30)],
|
|
|
|
(10, 10, 20, 40, 30, 30),
|
|
|
|
[10, 10, 20, 40, 30, 30],
|
|
|
|
)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
KITE_POINTS = (
|
|
|
|
((10, 50), (70, 10), (90, 50), (70, 90), (10, 50)),
|
|
|
|
[(10, 50), (70, 10), (90, 50), (70, 90), (10, 50)],
|
|
|
|
)
|
2017-05-15 17:09:29 +03:00
|
|
|
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
im = hopper("RGB").copy()
|
|
|
|
|
|
|
|
draw = ImageDraw.ImageDraw(im)
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
draw.ellipse(list(range(4)))
|
|
|
|
draw.line(list(range(10)))
|
|
|
|
draw.polygon(list(range(100)))
|
|
|
|
draw.rectangle(list(range(4)))
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_valueerror() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
with Image.open("Tests/images/chi.gif") as im:
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2020-02-12 19:29:19 +03:00
|
|
|
draw.line((0, 0), fill=(0, 0, 0))
|
2014-05-12 18:32:04 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_mode_mismatch() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
im = hopper("RGB").copy()
|
2015-12-30 01:37:50 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ImageDraw.ImageDraw(im, mode="L")
|
2015-12-30 01:37:50 +03:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("start, end", ((0, 180), (0.5, 180.4)))
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_arc(bbox: Coords, start: float, end: float) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2015-07-03 08:03:25 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
draw.arc(bbox, start, end)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_arc.png", 1)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_arc_end_le_start(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
start = 270.5
|
|
|
|
end = 0
|
2017-05-15 22:58:51 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.arc(bbox, start=start, end=end)
|
2017-05-15 22:58:51 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_arc_end_le_start.png")
|
2017-05-15 22:58:51 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_arc_no_loops(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# No need to go in loops
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
start = 5
|
|
|
|
end = 370
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.arc(bbox, start=start, end=end)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_arc_no_loops.png", 1)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2019-05-03 16:37:37 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_arc_width(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2019-05-03 16:37:37 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.arc(bbox, 10, 260, width=5)
|
2019-05-03 16:37:37 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_arc_width.png", 1)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_arc_width_pieslice_large(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Tests an arc with a large enough width that it is a pieslice
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.arc(bbox, 10, 260, fill="yellow", width=100)
|
2019-08-06 13:03:38 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_arc_width_pieslice.png", 1)
|
2019-08-06 13:03:38 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_arc_width_fill(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.arc(bbox, 10, 260, fill="yellow", width=5)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_arc_width_fill.png", 1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_arc_width_non_whole_angle(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = "Tests/images/imagedraw_arc_width_non_whole_angle.png"
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.arc(bbox, 10, 259.5, width=5)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_arc_high() -> None:
|
2020-06-29 23:16:06 +03:00
|
|
|
# Arrange
|
2020-06-29 23:58:58 +03:00
|
|
|
im = Image.new("RGB", (200, 200))
|
2020-06-29 23:16:06 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.arc([10, 10, 89, 189], 20, 330, width=20, fill="white")
|
|
|
|
draw.arc([110, 10, 189, 189], 20, 150, width=20, fill="white")
|
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_arc_high.png")
|
2020-06-29 23:16:06 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_bitmap() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
with Image.open("Tests/images/pil123rgba.png") as small:
|
2022-01-15 01:02:31 +03:00
|
|
|
small = small.resize((50, 50), Image.Resampling.NEAREST)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
|
|
|
# Act
|
2020-02-12 19:29:19 +03:00
|
|
|
draw.bitmap((10, 10), small)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_bitmap.png")
|
2018-04-13 08:37:54 +03:00
|
|
|
|
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("RGB", "L"))
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_chord(mode: str, bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new(mode, (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2020-07-16 12:43:29 +03:00
|
|
|
expected = f"Tests/images/imagedraw_chord_{mode}.png"
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-10-03 08:57:42 +03:00
|
|
|
draw.chord(bbox, 0, 180, fill="red", outline="yellow")
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_chord_width(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2015-06-05 17:46:50 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.chord(bbox, 10, 260, outline="yellow", width=5)
|
2015-06-05 17:46:50 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_chord_width.png", 1)
|
2018-05-27 08:32:22 +03:00
|
|
|
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_chord_width_fill(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.chord(bbox, 10, 260, fill="red", outline="yellow", width=5)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_chord_width_fill.png", 1)
|
2019-05-03 16:37:37 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_chord_zero_width(bbox: Coords) -> None:
|
2020-02-15 17:56:21 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2018-05-27 08:32:22 +03:00
|
|
|
|
2020-02-15 17:56:21 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.chord(bbox, 10, 260, fill="red", outline="yellow", width=0)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-15 17:56:21 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_chord_zero_width.png")
|
2018-04-13 08:37:54 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_chord_too_fat() -> None:
|
2020-06-29 23:16:06 +03:00
|
|
|
# Arrange
|
2020-06-29 23:58:58 +03:00
|
|
|
im = Image.new("RGB", (100, 100))
|
2020-06-29 23:16:06 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.chord([-150, -150, 99, 99], 15, 60, width=10, fill="white", outline="red")
|
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_chord_too_fat.png")
|
2020-06-29 23:16:06 +03:00
|
|
|
|
|
|
|
|
2024-05-27 14:59:32 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("RGB", "L"))
|
|
|
|
@pytest.mark.parametrize("xy", ((W / 2, H / 2), [W / 2, H / 2]))
|
|
|
|
def test_circle(mode: str, xy: Sequence[float]) -> None:
|
|
|
|
# Arrange
|
|
|
|
im = Image.new(mode, (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = f"Tests/images/imagedraw_ellipse_{mode}.png"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.circle(xy, 25, fill="green", outline="blue")
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
|
|
|
|
|
|
|
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("RGB", "L"))
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_ellipse(mode: str, bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new(mode, (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2020-07-16 12:43:29 +03:00
|
|
|
expected = f"Tests/images/imagedraw_ellipse_{mode}.png"
|
2019-05-03 16:37:37 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
draw.ellipse(bbox, fill="green", outline="blue")
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_ellipse_translucent(bbox: Coords) -> None:
|
2020-02-16 05:44:34 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im, "RGBA")
|
|
|
|
|
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.ellipse(bbox, fill=(0, 255, 0, 127))
|
2020-02-16 05:44:34 +03:00
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
expected = "Tests/images/imagedraw_ellipse_translucent.png"
|
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2020-02-16 05:44:34 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ellipse_edge() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2020-04-03 22:33:47 +03:00
|
|
|
draw.ellipse(((0, 0), (W - 1, H - 1)), fill="white")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_ellipse_edge.png", 1)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ellipse_symmetric() -> None:
|
2020-04-10 05:15:01 +03:00
|
|
|
for width, bbox in (
|
|
|
|
(100, (24, 24, 75, 75)),
|
|
|
|
(101, (25, 25, 75, 75)),
|
|
|
|
):
|
|
|
|
im = Image.new("RGB", (width, 100))
|
2017-01-30 22:50:48 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
2020-02-12 19:29:19 +03:00
|
|
|
draw.ellipse(bbox, fill="green", outline="blue")
|
2022-01-15 01:02:31 +03:00
|
|
|
assert_image_equal(im, im.transpose(Image.Transpose.FLIP_LEFT_RIGHT))
|
2017-01-30 22:50:48 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_ellipse_width(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2017-01-30 22:50:48 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.ellipse(bbox, outline="blue", width=5)
|
2017-01-31 09:22:33 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_ellipse_width.png", 1)
|
2017-01-31 09:22:33 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ellipse_width_large() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (500, 500))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2017-01-31 09:22:33 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
draw.ellipse((25, 25, 475, 475), outline="blue", width=75)
|
2017-01-30 22:50:48 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_ellipse_width_large.png", 1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_ellipse_width_fill(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.ellipse(bbox, fill="green", outline="blue", width=5)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_ellipse_width_fill.png", 1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_ellipse_zero_width(bbox: Coords) -> None:
|
2020-02-15 17:56:21 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-15 17:56:21 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.ellipse(bbox, fill="green", outline="blue", width=0)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-15 17:56:21 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_ellipse_zero_width.png")
|
2014-05-12 22:45:16 +04:00
|
|
|
|
|
|
|
|
2024-01-31 13:55:32 +03:00
|
|
|
def ellipse_various_sizes_helper(filled: bool) -> Image.Image:
|
2020-05-08 23:54:17 +03:00
|
|
|
ellipse_sizes = range(32)
|
|
|
|
image_size = sum(ellipse_sizes) + len(ellipse_sizes) + 1
|
|
|
|
im = Image.new("RGB", (image_size, image_size))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
x = 1
|
|
|
|
for w in ellipse_sizes:
|
|
|
|
y = 1
|
|
|
|
for h in ellipse_sizes:
|
2023-03-01 03:22:35 +03:00
|
|
|
x1 = x + w
|
|
|
|
if w:
|
|
|
|
x1 -= 1
|
|
|
|
y1 = y + h
|
|
|
|
if h:
|
|
|
|
y1 -= 1
|
|
|
|
border = [x, y, x1, y1]
|
2020-05-08 23:54:17 +03:00
|
|
|
if filled:
|
|
|
|
draw.ellipse(border, fill="white")
|
|
|
|
else:
|
|
|
|
draw.ellipse(border, outline="white")
|
|
|
|
y += h + 1
|
|
|
|
x += w + 1
|
|
|
|
|
|
|
|
return im
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ellipse_various_sizes() -> None:
|
2020-05-08 23:54:17 +03:00
|
|
|
im = ellipse_various_sizes_helper(False)
|
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_ellipse_various_sizes.png")
|
2020-05-08 23:54:17 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_ellipse_various_sizes_filled() -> None:
|
2020-05-08 23:54:17 +03:00
|
|
|
im = ellipse_various_sizes_helper(True)
|
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(
|
|
|
|
im, "Tests/images/imagedraw_ellipse_various_sizes_filled.png"
|
|
|
|
)
|
2020-05-08 23:54:17 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("points", POINTS)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_line(points: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
draw.line(points, fill="yellow", width=2)
|
2018-04-13 08:37:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_line.png")
|
2018-04-13 08:37:54 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_shape1() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (100, 100), "white")
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
x0, y0 = 5, 5
|
|
|
|
x1, y1 = 5, 50
|
|
|
|
x2, y2 = 95, 50
|
|
|
|
x3, y3 = 95, 5
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
s = ImageDraw.Outline()
|
|
|
|
s.move(x0, y0)
|
|
|
|
s.curve(x1, y1, x2, y2, x3, y3)
|
|
|
|
s.line(x0, y0)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
draw.shape(s, fill=1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_shape1.png")
|
2014-05-12 22:45:16 +04:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_shape2() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (100, 100), "white")
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
x0, y0 = 95, 95
|
|
|
|
x1, y1 = 95, 50
|
|
|
|
x2, y2 = 5, 50
|
|
|
|
x3, y3 = 5, 95
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
s = ImageDraw.Outline()
|
|
|
|
s.move(x0, y0)
|
|
|
|
s.curve(x1, y1, x2, y2, x3, y3)
|
|
|
|
s.line(x0, y0)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
draw.shape(s, outline="blue")
|
2017-05-15 18:24:12 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_shape2.png")
|
2017-05-15 18:24:12 +03:00
|
|
|
|
2017-05-15 17:09:29 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_transform() -> None:
|
2021-12-16 15:29:12 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (100, 100), "white")
|
|
|
|
expected = im.copy()
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
s = ImageDraw.Outline()
|
|
|
|
s.line(0, 0)
|
|
|
|
s.transform((0, 0, 0, 0, 0, 0))
|
|
|
|
|
|
|
|
draw.shape(s, fill=1)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal(im, expected)
|
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2022-10-03 08:57:42 +03:00
|
|
|
@pytest.mark.parametrize("start, end", ((-92, 46), (-92.2, 46.2)))
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_pieslice(bbox: Coords, start: float, end: float) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
draw.pieslice(bbox, start, end, fill="white", outline="blue")
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_pieslice.png", 1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_pieslice_width(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2018-04-12 18:15:27 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.pieslice(bbox, 10, 260, outline="blue", width=5)
|
2018-04-12 18:15:27 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_pieslice_width.png", 1)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_pieslice_width_fill(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = "Tests/images/imagedraw_pieslice_width_fill.png"
|
|
|
|
|
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.pieslice(bbox, 10, 260, fill="white", outline="blue", width=5)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_pieslice_zero_width(bbox: Coords) -> None:
|
2020-02-15 17:56:21 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.pieslice(bbox, 10, 260, fill="white", outline="blue", width=0)
|
2020-02-15 17:56:21 +03:00
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_pieslice_zero_width.png")
|
2020-02-15 17:56:21 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pieslice_wide() -> None:
|
2020-06-29 23:16:06 +03:00
|
|
|
# Arrange
|
2020-06-29 23:58:58 +03:00
|
|
|
im = Image.new("RGB", (200, 100))
|
2020-06-29 23:16:06 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.pieslice([0, 0, 199, 99], 190, 170, width=10, fill="white", outline="red")
|
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_pieslice_wide.png")
|
2020-06-29 23:16:06 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_pieslice_no_spikes() -> None:
|
2021-05-02 17:12:27 +03:00
|
|
|
im = Image.new("RGB", (161, 161), "white")
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
cxs = (
|
|
|
|
[140] * 3
|
|
|
|
+ list(range(140, 19, -20))
|
|
|
|
+ [20] * 5
|
|
|
|
+ list(range(20, 141, 20))
|
|
|
|
+ [140] * 2
|
|
|
|
)
|
|
|
|
cys = (
|
|
|
|
list(range(80, 141, 20))
|
|
|
|
+ [140] * 5
|
|
|
|
+ list(range(140, 19, -20))
|
|
|
|
+ [20] * 5
|
|
|
|
+ list(range(20, 80, 20))
|
|
|
|
)
|
|
|
|
|
|
|
|
for cx, cy, angle in zip(cxs, cys, range(0, 360, 15)):
|
|
|
|
draw.pieslice(
|
|
|
|
[cx - 100, cy - 100, cx + 100, cy + 100], angle, angle + 1, fill="black"
|
|
|
|
)
|
|
|
|
draw.point([cx, cy], fill="red")
|
|
|
|
|
|
|
|
im_pre_erase = im.copy()
|
|
|
|
draw.rectangle([21, 21, 139, 139], fill="white")
|
|
|
|
|
|
|
|
assert_image_equal(im, im_pre_erase)
|
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("points", POINTS)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_point(points: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.point(points, fill="yellow")
|
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_point.png")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_point_I16() -> None:
|
2023-07-05 01:49:47 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("I;16", (1, 1))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.point((0, 0), fill=0x1234)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert im.getpixel((0, 0)) == 0x1234
|
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("points", POINTS)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_polygon(points: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.polygon(points, fill="red", outline="blue")
|
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_polygon.png")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
@pytest.mark.parametrize("mode", ("RGB", "L"))
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("kite_points", KITE_POINTS)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_polygon_kite(
|
|
|
|
mode: str, kite_points: tuple[tuple[int, int], ...] | list[tuple[int, int]]
|
|
|
|
) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test drawing lines of different gradients (dx>dy, dy>dx) and
|
|
|
|
# vertical (dx==0) and horizontal (dy==0) lines
|
2022-08-23 14:41:32 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new(mode, (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = f"Tests/images/imagedraw_polygon_kite_{mode}.png"
|
2018-04-12 18:15:27 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.polygon(kite_points, fill="blue", outline="yellow")
|
2018-04-12 18:15:27 +03:00
|
|
|
|
2022-08-23 14:41:32 +03:00
|
|
|
# Assert
|
|
|
|
assert_image_equal_tofile(im, expected)
|
2018-04-12 18:15:27 +03:00
|
|
|
|
2019-06-23 00:33:55 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_polygon_1px_high() -> None:
|
2020-06-07 05:07:13 +03:00
|
|
|
# Test drawing a 1px high polygon
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (3, 3))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = "Tests/images/imagedraw_polygon_1px_high.png"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.polygon([(0, 1), (0, 1), (2, 1), (2, 1)], "#f00")
|
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, expected)
|
2020-06-07 05:07:13 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_polygon_1px_high_translucent() -> None:
|
2022-05-06 15:35:26 +03:00
|
|
|
# Test drawing a translucent 1px high polygon
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (4, 3))
|
|
|
|
draw = ImageDraw.Draw(im, "RGBA")
|
|
|
|
expected = "Tests/images/imagedraw_polygon_1px_high_translucent.png"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.polygon([(1, 1), (1, 1), (3, 1), (3, 1)], (255, 0, 0, 127))
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal_tofile(im, expected)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_polygon_translucent() -> None:
|
2021-11-16 03:12:48 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im, "RGBA")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.polygon([(20, 80), (80, 80), (80, 20)], fill=(0, 255, 0, 127))
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
expected = "Tests/images/imagedraw_polygon_translucent.png"
|
|
|
|
assert_image_equal_tofile(im, expected)
|
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_rectangle(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2019-06-23 00:33:55 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
draw.rectangle(bbox, fill="black", outline="green")
|
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_rectangle.png")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_big_rectangle() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test drawing a rectangle bigger than the image
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
bbox = [(-1, -1), (W + 1, H + 1)]
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.rectangle(bbox, fill="orange")
|
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_big_rectangle.png", 1)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_rectangle_width(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = "Tests/images/imagedraw_rectangle_width.png"
|
2019-06-23 00:33:55 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rectangle(bbox, outline="green", width=5)
|
2014-05-13 00:27:02 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, expected)
|
2014-05-13 00:27:02 +04:00
|
|
|
|
2018-09-26 15:22:17 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_rectangle_width_fill(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = "Tests/images/imagedraw_rectangle_width_fill.png"
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rectangle(bbox, fill="blue", outline="green", width=5)
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, expected)
|
2017-03-01 12:20:18 +03:00
|
|
|
|
2018-09-01 10:18:13 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_rectangle_zero_width(bbox: Coords) -> None:
|
2020-02-15 17:56:21 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rectangle(bbox, fill="blue", outline="green", width=0)
|
2020-02-15 17:56:21 +03:00
|
|
|
|
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_rectangle_zero_width.png")
|
2020-02-15 17:56:21 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_rectangle_I16(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("I;16", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-13 00:27:02 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
2023-07-05 01:49:47 +03:00
|
|
|
draw.rectangle(bbox, outline=0xFFFF)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
# Assert
|
2024-03-02 07:39:43 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_rectangle_I.tiff")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_rectangle_translucent_outline(bbox: Coords) -> None:
|
2021-02-21 23:48:58 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im, "RGBA")
|
|
|
|
|
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rectangle(bbox, fill="black", outline=(0, 255, 0, 127), width=5)
|
2021-02-21 23:48:58 +03:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
im, "Tests/images/imagedraw_rectangle_translucent_outline.png"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-03-08 11:53:59 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"xy",
|
|
|
|
[(10, 20, 190, 180), ([10, 20], [190, 180]), ((10, 20), (190, 180))],
|
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_rounded_rectangle(
|
2024-02-07 02:19:00 +03:00
|
|
|
xy: (
|
|
|
|
tuple[int, int, int, int]
|
|
|
|
| tuple[list[int]]
|
|
|
|
| tuple[tuple[int, int], tuple[int, int]]
|
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
) -> None:
|
2021-03-08 11:53:59 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (200, 200))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.rounded_rectangle(xy, 30, fill="red", outline="green", width=5)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_rounded_rectangle.png")
|
|
|
|
|
|
|
|
|
2023-02-16 11:19:17 +03:00
|
|
|
@pytest.mark.parametrize("top_left", (True, False))
|
|
|
|
@pytest.mark.parametrize("top_right", (True, False))
|
|
|
|
@pytest.mark.parametrize("bottom_right", (True, False))
|
|
|
|
@pytest.mark.parametrize("bottom_left", (True, False))
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_rounded_rectangle_corners(
|
2024-01-31 13:55:32 +03:00
|
|
|
top_left: bool, top_right: bool, bottom_right: bool, bottom_left: bool
|
2024-01-31 12:12:58 +03:00
|
|
|
) -> None:
|
2023-02-16 11:19:17 +03:00
|
|
|
corners = (top_left, top_right, bottom_right, bottom_left)
|
|
|
|
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (200, 200))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.rounded_rectangle(
|
|
|
|
(10, 20, 190, 180), 30, fill="red", outline="green", width=5, corners=corners
|
|
|
|
)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
suffix = "".join(
|
|
|
|
(
|
|
|
|
("y" if top_left else "n"),
|
|
|
|
("y" if top_right else "n"),
|
|
|
|
("y" if bottom_right else "n"),
|
|
|
|
("y" if bottom_left else "n"),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
im, "Tests/images/imagedraw_rounded_rectangle_corners_" + suffix + ".png"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-04-05 01:54:06 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"xy, radius, type",
|
|
|
|
[
|
|
|
|
((10, 20, 190, 180), 30.5, "given"),
|
|
|
|
((10, 10, 181, 190), 90, "width"),
|
|
|
|
((10, 20, 190, 181), 85, "height"),
|
|
|
|
],
|
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_rounded_rectangle_non_integer_radius(
|
|
|
|
xy: tuple[int, int, int, int], radius: float, type: str
|
|
|
|
) -> None:
|
2021-04-05 01:54:06 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (200, 200))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.rounded_rectangle(xy, radius, fill="red", outline="green", width=5)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
im,
|
|
|
|
"Tests/images/imagedraw_rounded_rectangle_non_integer_radius_" + type + ".png",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_rounded_rectangle_zero_radius(bbox: Coords) -> None:
|
2021-03-08 11:53:59 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rounded_rectangle(bbox, 0, fill="blue", outline="green", width=5)
|
2021-03-08 11:53:59 +03:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_rectangle_width_fill.png")
|
|
|
|
|
|
|
|
|
2021-02-21 23:38:04 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"xy, suffix",
|
|
|
|
[
|
|
|
|
((20, 10, 80, 90), "x"),
|
2023-05-10 06:25:35 +03:00
|
|
|
((20, 10, 81, 90), "x_odd"),
|
2024-03-15 15:58:07 +03:00
|
|
|
((20, 10, 81.1, 90), "x_odd"),
|
2021-02-21 23:38:04 +03:00
|
|
|
((10, 20, 90, 80), "y"),
|
2023-05-10 06:25:35 +03:00
|
|
|
((10, 20, 90, 81), "y_odd"),
|
2024-03-15 15:58:07 +03:00
|
|
|
((10, 20, 90, 81.1), "y_odd"),
|
2021-02-21 23:38:04 +03:00
|
|
|
((20, 20, 80, 80), "both"),
|
|
|
|
],
|
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_rounded_rectangle_translucent(
|
|
|
|
xy: tuple[int, int, int, int], suffix: str
|
|
|
|
) -> None:
|
2021-02-21 23:38:04 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im, "RGBA")
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.rounded_rectangle(
|
|
|
|
xy, 30, fill=(255, 0, 0, 127), outline=(0, 255, 0, 127), width=5
|
|
|
|
)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
im, "Tests/images/imagedraw_rounded_rectangle_" + suffix + ".png"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_floodfill(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
red = ImageColor.getrgb("red")
|
|
|
|
|
|
|
|
for mode, value in [("L", 1), ("RGBA", (255, 0, 0, 0)), ("RGB", red)]:
|
2014-06-10 13:10:47 +04:00
|
|
|
# Arrange
|
2020-02-12 19:29:19 +03:00
|
|
|
im = Image.new(mode, (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rectangle(bbox, outline="yellow", fill="green")
|
2019-06-13 18:54:46 +03:00
|
|
|
centre_point = (int(W / 2), int(H / 2))
|
2014-06-06 09:02:18 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
2020-02-12 19:29:19 +03:00
|
|
|
ImageDraw.floodfill(im, centre_point, value)
|
2014-05-13 00:33:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
2020-02-12 19:29:19 +03:00
|
|
|
expected = "Tests/images/imagedraw_floodfill_" + mode + ".png"
|
|
|
|
with Image.open(expected) as im_floodfill:
|
|
|
|
assert_image_equal(im, im_floodfill)
|
2014-05-13 00:33:05 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test that using the same colour does not change the image
|
|
|
|
ImageDraw.floodfill(im, centre_point, red)
|
|
|
|
assert_image_equal(im, im_floodfill)
|
2017-07-01 12:37:02 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test that filling outside the image does not change the image
|
|
|
|
ImageDraw.floodfill(im, (W, H), red)
|
|
|
|
assert_image_equal(im, im_floodfill)
|
2017-07-01 12:37:02 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test filling at the edge of an image
|
|
|
|
im = Image.new("RGB", (1, 1))
|
|
|
|
ImageDraw.floodfill(im, (0, 0), red)
|
|
|
|
assert_image_equal(im, Image.new("RGB", (1, 1), red))
|
2017-08-31 14:51:21 +03:00
|
|
|
|
2017-07-01 12:37:02 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_floodfill_border(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# floodfill() is experimental
|
2019-08-09 22:51:10 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rectangle(bbox, outline="yellow", fill="green")
|
2020-02-12 19:29:19 +03:00
|
|
|
centre_point = (int(W / 2), int(H / 2))
|
2019-08-09 22:51:10 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ImageDraw.floodfill(
|
2020-09-01 20:16:46 +03:00
|
|
|
im,
|
|
|
|
centre_point,
|
|
|
|
ImageColor.getrgb("red"),
|
|
|
|
border=ImageColor.getrgb("black"),
|
2020-02-12 19:29:19 +03:00
|
|
|
)
|
2019-08-09 22:51:10 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_floodfill2.png")
|
2019-08-09 22:51:10 +03:00
|
|
|
|
2017-05-15 21:32:54 +03:00
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_floodfill_thresh(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# floodfill() is experimental
|
2017-05-15 21:32:54 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2022-12-31 07:42:38 +03:00
|
|
|
draw.rectangle(bbox, outline="darkgreen", fill="green")
|
2020-02-12 19:29:19 +03:00
|
|
|
centre_point = (int(W / 2), int(H / 2))
|
2014-06-25 03:48:38 +04:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ImageDraw.floodfill(im, centre_point, ImageColor.getrgb("red"), thresh=30)
|
2018-09-16 14:29:09 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_floodfill2.png")
|
2018-09-16 14:29:09 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_floodfill_not_negative() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# floodfill() is experimental
|
|
|
|
# Test that floodfill does not extend into negative coordinates
|
2017-10-04 09:54:53 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.line((W / 2, 0, W / 2, H / 2), fill="green")
|
|
|
|
draw.line((0, H / 2, W / 2, H / 2), fill="green")
|
2019-07-28 23:40:03 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Act
|
|
|
|
ImageDraw.floodfill(im, (int(W / 4), int(H / 4)), ImageColor.getrgb("red"))
|
2019-07-28 23:40:03 +03:00
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
# Assert
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_floodfill_not_negative.png")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
|
|
|
def create_base_image_draw(
|
2024-01-31 13:55:32 +03:00
|
|
|
size: tuple[int, int],
|
|
|
|
mode: str = DEFAULT_MODE,
|
|
|
|
background1: tuple[int, int, int] = WHITE,
|
|
|
|
background2: tuple[int, int, int] = GRAY,
|
|
|
|
) -> tuple[Image.Image, ImageDraw.ImageDraw]:
|
2020-02-12 19:29:19 +03:00
|
|
|
img = Image.new(mode, size, background1)
|
|
|
|
for x in range(0, size[0]):
|
|
|
|
for y in range(0, size[1]):
|
|
|
|
if (x + y) % 2 == 0:
|
|
|
|
img.putpixel((x, y), background2)
|
|
|
|
return img, ImageDraw.Draw(img)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_square() -> None:
|
2021-03-20 14:32:27 +03:00
|
|
|
expected = os.path.join(IMAGES_PATH, "square.png")
|
|
|
|
img, draw = create_base_image_draw((10, 10))
|
|
|
|
draw.polygon([(2, 2), (2, 7), (7, 7), (7, 2)], BLACK)
|
|
|
|
assert_image_equal_tofile(img, expected, "square as normal polygon failed")
|
|
|
|
img, draw = create_base_image_draw((10, 10))
|
|
|
|
draw.polygon([(7, 7), (7, 2), (2, 2), (2, 7)], BLACK)
|
|
|
|
assert_image_equal_tofile(img, expected, "square as inverted polygon failed")
|
|
|
|
img, draw = create_base_image_draw((10, 10))
|
|
|
|
draw.rectangle((2, 2, 7, 7), BLACK)
|
|
|
|
assert_image_equal_tofile(img, expected, "square as normal rectangle failed")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_triangle_right() -> None:
|
2021-02-21 14:15:56 +03:00
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.polygon([(3, 5), (17, 5), (10, 12)], BLACK)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, os.path.join(IMAGES_PATH, "triangle_right.png"), "triangle right failed"
|
|
|
|
)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2021-11-16 14:02:54 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"fill, suffix",
|
|
|
|
((BLACK, "width"), (None, "width_no_fill")),
|
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_triangle_right_width(fill: tuple[int, int, int] | None, suffix: str) -> None:
|
2021-11-16 14:02:54 +03:00
|
|
|
img, draw = create_base_image_draw((100, 100))
|
|
|
|
draw.polygon([(15, 25), (85, 25), (50, 60)], fill, WHITE, width=5)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, os.path.join(IMAGES_PATH, "triangle_right_" + suffix + ".png")
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_line_horizontal() -> None:
|
2021-02-21 14:15:56 +03:00
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 5, 14, 5), BLACK, 2)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_horizontal_w2px_normal.png"),
|
|
|
|
"line straight horizontal normal 2px wide failed",
|
|
|
|
)
|
|
|
|
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((14, 5, 5, 5), BLACK, 2)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_horizontal_w2px_inverted.png"),
|
|
|
|
"line straight horizontal inverted 2px wide failed",
|
|
|
|
)
|
2021-03-20 14:32:27 +03:00
|
|
|
|
|
|
|
expected = os.path.join(IMAGES_PATH, "line_horizontal_w3px.png")
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 5, 14, 5), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, expected, "line straight horizontal normal 3px wide failed"
|
|
|
|
)
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((14, 5, 5, 5), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, expected, "line straight horizontal inverted 3px wide failed"
|
|
|
|
)
|
2021-02-21 14:15:56 +03:00
|
|
|
|
|
|
|
img, draw = create_base_image_draw((200, 110))
|
|
|
|
draw.line((5, 55, 195, 55), BLACK, 101)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_horizontal_w101px.png"),
|
|
|
|
"line straight horizontal 101px wide failed",
|
|
|
|
)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-06-01 14:31:53 +03:00
|
|
|
@pytest.mark.xfail(reason="failing test")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_line_h_s1_w2() -> None:
|
2021-02-21 14:15:56 +03:00
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 5, 14, 6), BLACK, 2)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_horizontal_slope1px_w2px.png"),
|
|
|
|
"line horizontal 1px slope 2px wide failed",
|
|
|
|
)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_line_vertical() -> None:
|
2021-02-21 14:15:56 +03:00
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 5, 5, 14), BLACK, 2)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_vertical_w2px_normal.png"),
|
|
|
|
"line straight vertical normal 2px wide failed",
|
|
|
|
)
|
|
|
|
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 14, 5, 5), BLACK, 2)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_vertical_w2px_inverted.png"),
|
|
|
|
"line straight vertical inverted 2px wide failed",
|
|
|
|
)
|
2021-03-20 14:32:27 +03:00
|
|
|
|
|
|
|
expected = os.path.join(IMAGES_PATH, "line_vertical_w3px.png")
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 5, 5, 14), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, expected, "line straight vertical normal 3px wide failed"
|
|
|
|
)
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 14, 5, 5), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, expected, "line straight vertical inverted 3px wide failed"
|
|
|
|
)
|
|
|
|
|
2021-02-21 14:15:56 +03:00
|
|
|
img, draw = create_base_image_draw((110, 200))
|
|
|
|
draw.line((55, 5, 55, 195), BLACK, 101)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_vertical_w101px.png"),
|
|
|
|
"line straight vertical 101px wide failed",
|
|
|
|
)
|
|
|
|
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 5, 6, 14), BLACK, 2)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img,
|
|
|
|
os.path.join(IMAGES_PATH, "line_vertical_slope1px_w2px.png"),
|
|
|
|
"line vertical 1px slope 2px wide failed",
|
|
|
|
)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_line_oblique_45() -> None:
|
2021-03-20 14:32:27 +03:00
|
|
|
expected = os.path.join(IMAGES_PATH, "line_oblique_45_w3px_a.png")
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 5, 14, 14), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(img, expected, "line oblique 45 normal 3px wide A failed")
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((14, 14, 5, 5), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, expected, "line oblique 45 inverted 3px wide A failed"
|
|
|
|
)
|
|
|
|
|
|
|
|
expected = os.path.join(IMAGES_PATH, "line_oblique_45_w3px_b.png")
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((14, 5, 5, 14), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(img, expected, "line oblique 45 normal 3px wide B failed")
|
|
|
|
img, draw = create_base_image_draw((20, 20))
|
|
|
|
draw.line((5, 14, 14, 5), BLACK, 3)
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, expected, "line oblique 45 inverted 3px wide B failed"
|
|
|
|
)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_wide_line_dot() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Test drawing a wide "line" from one point to another just draws a single point
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.line([(50, 50), (50, 50)], width=3)
|
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_wide_line_dot.png", 1)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_wide_line_larger_than_int() -> None:
|
2020-06-23 12:17:00 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
expected = "Tests/images/imagedraw_wide_line_larger_than_int.png"
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.line([(0, 0), (32768, 32768)], width=3)
|
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2020-06-23 12:17:00 +03:00
|
|
|
|
|
|
|
|
2020-06-04 00:56:14 +03:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"xy",
|
|
|
|
[
|
2020-04-24 11:48:10 +03:00
|
|
|
[
|
|
|
|
(400, 280),
|
|
|
|
(380, 280),
|
|
|
|
(450, 280),
|
|
|
|
(440, 120),
|
|
|
|
(350, 200),
|
|
|
|
(310, 280),
|
|
|
|
(300, 280),
|
|
|
|
(250, 280),
|
|
|
|
(250, 200),
|
|
|
|
(150, 200),
|
|
|
|
(150, 260),
|
|
|
|
(50, 200),
|
|
|
|
(150, 50),
|
|
|
|
(250, 100),
|
|
|
|
],
|
|
|
|
(
|
|
|
|
400,
|
|
|
|
280,
|
|
|
|
380,
|
|
|
|
280,
|
|
|
|
450,
|
|
|
|
280,
|
|
|
|
440,
|
|
|
|
120,
|
|
|
|
350,
|
|
|
|
200,
|
|
|
|
310,
|
|
|
|
280,
|
|
|
|
300,
|
|
|
|
280,
|
|
|
|
250,
|
|
|
|
280,
|
|
|
|
250,
|
|
|
|
200,
|
|
|
|
150,
|
|
|
|
200,
|
|
|
|
150,
|
|
|
|
260,
|
|
|
|
50,
|
|
|
|
200,
|
|
|
|
150,
|
|
|
|
50,
|
|
|
|
250,
|
|
|
|
100,
|
|
|
|
),
|
|
|
|
[
|
|
|
|
400,
|
|
|
|
280,
|
|
|
|
380,
|
|
|
|
280,
|
|
|
|
450,
|
|
|
|
280,
|
|
|
|
440,
|
|
|
|
120,
|
|
|
|
350,
|
|
|
|
200,
|
|
|
|
310,
|
|
|
|
280,
|
|
|
|
300,
|
|
|
|
280,
|
|
|
|
250,
|
|
|
|
280,
|
|
|
|
250,
|
|
|
|
200,
|
|
|
|
150,
|
|
|
|
200,
|
|
|
|
150,
|
|
|
|
260,
|
|
|
|
50,
|
|
|
|
200,
|
|
|
|
150,
|
|
|
|
50,
|
|
|
|
250,
|
|
|
|
100,
|
|
|
|
],
|
2020-06-04 00:56:14 +03:00
|
|
|
],
|
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_line_joint(xy: list[tuple[int, int]] | tuple[int, ...] | list[int]) -> None:
|
2020-06-04 00:56:14 +03:00
|
|
|
im = Image.new("RGB", (500, 325))
|
|
|
|
draw = ImageDraw.Draw(im)
|
2020-04-24 11:48:10 +03:00
|
|
|
|
2020-06-04 00:56:14 +03:00
|
|
|
# Act
|
|
|
|
draw.line(xy, GRAY, 50, "curve")
|
2020-04-24 11:48:10 +03:00
|
|
|
|
2020-06-04 00:56:14 +03:00
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_line_joint_curve.png", 3)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_textsize_empty_string() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# https://github.com/python-pillow/Pillow/issues/2783
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
# Should not cause 'SystemError: <built-in method getsize of
|
|
|
|
# ImagingFont object at 0x...> returned NULL without setting an error'
|
2022-05-25 20:06:20 +03:00
|
|
|
draw.textbbox((0, 0), "")
|
|
|
|
draw.textbbox((0, 0), "\n")
|
|
|
|
draw.textbbox((0, 0), "test\n")
|
|
|
|
draw.textlength("")
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_textbbox_stroke() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 20)
|
|
|
|
|
|
|
|
# Act / Assert
|
2022-05-25 20:06:20 +03:00
|
|
|
assert draw.textbbox((2, 2), "A", font, stroke_width=2) == (0, 4, 16, 20)
|
|
|
|
assert draw.textbbox((2, 2), "A", font, stroke_width=4) == (-2, 2, 18, 22)
|
|
|
|
assert draw.textbbox((2, 2), "ABC\nAaaa", font, stroke_width=2) == (0, 4, 52, 44)
|
|
|
|
assert draw.textbbox((2, 2), "ABC\nAaaa", font, stroke_width=4) == (-2, 2, 54, 50)
|
|
|
|
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_stroke() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
for suffix, stroke_fill in {"same": None, "different": "#0f0"}.items():
|
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (120, 130))
|
2019-07-28 23:40:03 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)
|
|
|
|
|
|
|
|
# Act
|
2020-04-22 04:02:08 +03:00
|
|
|
draw.text((12, 12), "A", "#f00", font, stroke_width=2, stroke_fill=stroke_fill)
|
2019-07-28 23:40:03 +03:00
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(
|
|
|
|
im, "Tests/images/imagedraw_stroke_" + suffix + ".png", 3.1
|
2019-07-28 23:40:03 +03:00
|
|
|
)
|
|
|
|
|
2020-02-12 19:29:19 +03:00
|
|
|
|
2020-02-16 12:40:23 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_stroke_descender() -> None:
|
2020-02-16 12:40:23 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (120, 130))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)
|
|
|
|
|
|
|
|
# Act
|
2020-04-22 04:02:08 +03:00
|
|
|
draw.text((12, 2), "y", "#f00", font, stroke_width=2, stroke_fill="#0f0")
|
2020-02-16 12:40:23 +03:00
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_stroke_descender.png", 6.76)
|
2020-02-16 12:40:23 +03:00
|
|
|
|
|
|
|
|
2022-11-08 03:49:39 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_split_word() -> None:
|
2022-11-08 03:49:39 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (230, 55))
|
|
|
|
expected = im.copy()
|
|
|
|
expected_draw = ImageDraw.Draw(expected)
|
|
|
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 48)
|
|
|
|
expected_draw.text((0, 0), "paradise", font=font)
|
|
|
|
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.text((0, 0), "par", font=font)
|
|
|
|
|
|
|
|
length = draw.textlength("par", font=font)
|
|
|
|
draw.text((length, 0), "adise", font=font)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert_image_equal(im, expected)
|
|
|
|
|
|
|
|
|
2020-02-18 01:03:32 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_stroke_multiline() -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (100, 250))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw.multiline_text(
|
2020-04-22 04:02:08 +03:00
|
|
|
(12, 12), "A\nB", "#f00", font, stroke_width=2, stroke_fill="#0f0"
|
2020-02-12 19:29:19 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
# Assert
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, "Tests/images/imagedraw_stroke_multiline.png", 3.3)
|
2020-02-12 19:29:19 +03:00
|
|
|
|
|
|
|
|
2023-08-24 12:02:27 +03:00
|
|
|
@skip_unless_feature("freetype2")
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_setting_default_font() -> None:
|
2022-08-06 10:29:44 +03:00
|
|
|
# Arrange
|
|
|
|
im = Image.new("RGB", (100, 250))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
ImageDraw.ImageDraw.font = font
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
try:
|
|
|
|
assert draw.getfont() == font
|
|
|
|
finally:
|
|
|
|
ImageDraw.ImageDraw.font = None
|
2023-08-26 10:00:34 +03:00
|
|
|
assert isinstance(draw.getfont(), ImageFont.load_default().__class__)
|
2022-08-06 10:29:44 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_default_font_size() -> None:
|
2023-08-26 10:00:04 +03:00
|
|
|
freetype_support = features.check_module("freetype2")
|
|
|
|
text = "Default font at a specific size."
|
|
|
|
|
|
|
|
im = Image.new("RGB", (220, 25))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
with contextlib.nullcontext() if freetype_support else pytest.raises(ImportError):
|
|
|
|
draw.text((0, 0), text, font_size=16)
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_default_font_size.png")
|
|
|
|
|
|
|
|
with contextlib.nullcontext() if freetype_support else pytest.raises(ImportError):
|
|
|
|
assert draw.textlength(text, font_size=16) == 216
|
|
|
|
|
|
|
|
with contextlib.nullcontext() if freetype_support else pytest.raises(ImportError):
|
|
|
|
assert draw.textbbox((0, 0), text, font_size=16) == (0, 3, 216, 19)
|
|
|
|
|
|
|
|
im = Image.new("RGB", (220, 25))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
with contextlib.nullcontext() if freetype_support else pytest.raises(ImportError):
|
|
|
|
draw.multiline_text((0, 0), text, font_size=16)
|
|
|
|
assert_image_equal_tofile(im, "Tests/images/imagedraw_default_font_size.png")
|
|
|
|
|
|
|
|
with contextlib.nullcontext() if freetype_support else pytest.raises(ImportError):
|
|
|
|
assert draw.multiline_textbbox((0, 0), text, font_size=16) == (0, 3, 216, 19)
|
|
|
|
|
|
|
|
|
2022-12-31 07:42:38 +03:00
|
|
|
@pytest.mark.parametrize("bbox", BBOX)
|
2024-02-05 23:49:43 +03:00
|
|
|
def test_same_color_outline(bbox: Coords) -> None:
|
2020-02-12 19:29:19 +03:00
|
|
|
# Prepare shape
|
|
|
|
x0, y0 = 5, 5
|
|
|
|
x1, y1 = 5, 50
|
|
|
|
x2, y2 = 95, 50
|
|
|
|
x3, y3 = 95, 5
|
|
|
|
|
|
|
|
s = ImageDraw.Outline()
|
|
|
|
s.move(x0, y0)
|
|
|
|
s.curve(x1, y1, x2, y2, x3, y3)
|
|
|
|
s.line(x0, y0)
|
|
|
|
|
|
|
|
# Begin
|
|
|
|
for mode in ["RGB", "L"]:
|
2024-01-31 13:55:32 +03:00
|
|
|
fill = "red"
|
|
|
|
for outline in [None, "red", "#f00"]:
|
2020-02-12 19:29:19 +03:00
|
|
|
for operation, args in {
|
2022-12-31 07:42:38 +03:00
|
|
|
"chord": [bbox, 0, 180],
|
|
|
|
"ellipse": [bbox],
|
2020-02-12 19:29:19 +03:00
|
|
|
"shape": [s],
|
2022-12-31 07:42:38 +03:00
|
|
|
"pieslice": [bbox, -90, 45],
|
2020-02-12 19:29:19 +03:00
|
|
|
"polygon": [[(18, 30), (85, 30), (60, 72)]],
|
2022-12-31 07:42:38 +03:00
|
|
|
"rectangle": [bbox],
|
2020-02-12 19:29:19 +03:00
|
|
|
}.items():
|
|
|
|
# Arrange
|
|
|
|
im = Image.new(mode, (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
|
|
|
|
# Act
|
|
|
|
draw_method = getattr(draw, operation)
|
2024-01-31 13:55:32 +03:00
|
|
|
assert isinstance(args, list)
|
2020-02-12 19:29:19 +03:00
|
|
|
args += [fill, outline]
|
|
|
|
draw_method(*args)
|
|
|
|
|
|
|
|
# Assert
|
2020-07-16 12:43:29 +03:00
|
|
|
expected = f"Tests/images/imagedraw_outline_{operation}_{mode}.png"
|
2020-06-23 11:20:43 +03:00
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2020-08-09 23:43:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-05-03 15:54:18 +03:00
|
|
|
"n_sides, polygon_name, args",
|
|
|
|
[
|
|
|
|
(4, "square", {}),
|
|
|
|
(8, "regular_octagon", {}),
|
|
|
|
(4, "square_rotate_45", {"rotation": 45}),
|
|
|
|
(3, "triangle_width", {"width": 5, "outline": "yellow"}),
|
|
|
|
],
|
2020-08-09 23:43:37 +03:00
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_draw_regular_polygon(
|
|
|
|
n_sides: int, polygon_name: str, args: dict[str, int | str]
|
|
|
|
) -> None:
|
2020-08-09 23:43:37 +03:00
|
|
|
im = Image.new("RGBA", size=(W, H), color=(255, 0, 0, 0))
|
2023-05-03 15:54:18 +03:00
|
|
|
filename = f"Tests/images/imagedraw_{polygon_name}.png"
|
2020-08-09 23:43:37 +03:00
|
|
|
draw = ImageDraw.Draw(im)
|
2020-08-21 12:12:00 +03:00
|
|
|
bounding_circle = ((W // 2, H // 2), 25)
|
2023-05-03 15:54:18 +03:00
|
|
|
draw.regular_polygon(bounding_circle, n_sides, fill="red", **args)
|
2021-02-21 14:15:56 +03:00
|
|
|
assert_image_equal_tofile(im, filename)
|
2020-08-09 23:43:37 +03:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2020-08-20 22:46:11 +03:00
|
|
|
"n_sides, expected_vertices",
|
2020-08-09 23:43:37 +03:00
|
|
|
[
|
2020-08-20 22:46:11 +03:00
|
|
|
(3, [(28.35, 62.5), (71.65, 62.5), (50.0, 25.0)]),
|
|
|
|
(4, [(32.32, 67.68), (67.68, 67.68), (67.68, 32.32), (32.32, 32.32)]),
|
2020-08-09 23:43:37 +03:00
|
|
|
(
|
|
|
|
5,
|
|
|
|
[
|
2020-08-20 22:46:11 +03:00
|
|
|
(35.31, 70.23),
|
|
|
|
(64.69, 70.23),
|
|
|
|
(73.78, 42.27),
|
|
|
|
(50.0, 25.0),
|
|
|
|
(26.22, 42.27),
|
2020-08-09 23:43:37 +03:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
6,
|
|
|
|
[
|
2020-08-20 22:46:11 +03:00
|
|
|
(37.5, 71.65),
|
|
|
|
(62.5, 71.65),
|
|
|
|
(75.0, 50.0),
|
|
|
|
(62.5, 28.35),
|
|
|
|
(37.5, 28.35),
|
|
|
|
(25.0, 50.0),
|
2020-08-09 23:43:37 +03:00
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_compute_regular_polygon_vertices(
|
|
|
|
n_sides: int, expected_vertices: list[tuple[float, float]]
|
|
|
|
) -> None:
|
2020-08-21 12:12:00 +03:00
|
|
|
bounding_circle = (W // 2, H // 2, 25)
|
|
|
|
vertices = ImageDraw._compute_regular_polygon_vertices(bounding_circle, n_sides, 0)
|
2020-08-09 23:43:37 +03:00
|
|
|
assert vertices == expected_vertices
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2020-08-21 12:12:00 +03:00
|
|
|
"n_sides, bounding_circle, rotation, expected_error, error_message",
|
2020-08-09 23:43:37 +03:00
|
|
|
[
|
2020-08-21 12:12:00 +03:00
|
|
|
(None, (50, 50, 25), 0, TypeError, "n_sides should be an int"),
|
|
|
|
(1, (50, 50, 25), 0, ValueError, "n_sides should be an int > 2"),
|
2024-02-05 12:36:34 +03:00
|
|
|
(3, 50, 0, TypeError, "bounding_circle should be a sequence"),
|
2020-08-21 12:12:00 +03:00
|
|
|
(
|
|
|
|
3,
|
|
|
|
(50, 50, 100, 100),
|
|
|
|
0,
|
|
|
|
ValueError,
|
|
|
|
"bounding_circle should contain 2D coordinates "
|
|
|
|
"and a radius (e.g. (x, y, r) or ((x, y), r) )",
|
|
|
|
),
|
2020-08-09 23:43:37 +03:00
|
|
|
(
|
|
|
|
3,
|
2020-08-21 12:12:00 +03:00
|
|
|
(50, 50, "25"),
|
2020-08-09 23:43:37 +03:00
|
|
|
0,
|
|
|
|
ValueError,
|
2020-08-21 12:12:00 +03:00
|
|
|
"bounding_circle should only contain numeric data",
|
2020-08-09 23:43:37 +03:00
|
|
|
),
|
|
|
|
(
|
|
|
|
3,
|
2020-08-21 12:12:00 +03:00
|
|
|
((50, 50, 50), 25),
|
2020-08-09 23:43:37 +03:00
|
|
|
0,
|
|
|
|
ValueError,
|
2020-08-21 12:12:00 +03:00
|
|
|
"bounding_circle centre should contain 2D coordinates (e.g. (x, y))",
|
2020-08-09 23:43:37 +03:00
|
|
|
),
|
2020-09-05 06:32:59 +03:00
|
|
|
(
|
|
|
|
3,
|
|
|
|
(50, 50, 0),
|
|
|
|
0,
|
|
|
|
ValueError,
|
|
|
|
"bounding_circle radius should be > 0",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
3,
|
|
|
|
(50, 50, 25),
|
|
|
|
"0",
|
|
|
|
ValueError,
|
|
|
|
"rotation should be an int or float",
|
|
|
|
),
|
2020-08-09 23:43:37 +03:00
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_compute_regular_polygon_vertices_input_error_handling(
|
2024-06-09 08:16:17 +03:00
|
|
|
n_sides: int,
|
|
|
|
bounding_circle: int | tuple[int | tuple[int] | str, ...],
|
|
|
|
rotation: int | str,
|
|
|
|
expected_error: type[Exception],
|
|
|
|
error_message: str,
|
2024-01-31 12:12:58 +03:00
|
|
|
) -> None:
|
2020-08-09 23:43:37 +03:00
|
|
|
with pytest.raises(expected_error) as e:
|
2020-08-21 12:12:00 +03:00
|
|
|
ImageDraw._compute_regular_polygon_vertices(bounding_circle, n_sides, rotation)
|
2020-08-09 23:43:37 +03:00
|
|
|
assert str(e.value) == error_message
|
2021-01-31 08:38:46 +03:00
|
|
|
|
2021-01-31 09:44:14 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_continuous_horizontal_edges_polygon() -> None:
|
2021-03-20 14:42:10 +03:00
|
|
|
xy = [
|
|
|
|
(2, 6),
|
2021-03-20 17:25:17 +03:00
|
|
|
(6, 6),
|
2021-03-20 14:42:10 +03:00
|
|
|
(12, 6),
|
|
|
|
(12, 12),
|
|
|
|
(8, 12),
|
|
|
|
(8, 8),
|
|
|
|
(4, 8),
|
|
|
|
(2, 8),
|
|
|
|
]
|
|
|
|
img, draw = create_base_image_draw((16, 16))
|
|
|
|
draw.polygon(xy, BLACK)
|
|
|
|
expected = os.path.join(IMAGES_PATH, "continuous_horizontal_edges_polygon.png")
|
|
|
|
assert_image_equal_tofile(
|
|
|
|
img, expected, "continuous horizontal edges polygon failed"
|
|
|
|
)
|
2022-01-21 03:55:59 +03:00
|
|
|
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_discontiguous_corners_polygon() -> None:
|
2022-01-21 03:55:59 +03:00
|
|
|
img, draw = create_base_image_draw((84, 68))
|
|
|
|
draw.polygon(((1, 21), (34, 4), (71, 1), (38, 18)), BLACK)
|
|
|
|
draw.polygon(((71, 44), (38, 27), (1, 24)), BLACK)
|
|
|
|
draw.polygon(
|
|
|
|
((38, 66), (5, 49), (77, 49), (47, 66), (82, 63), (82, 47), (1, 47), (1, 63)),
|
|
|
|
BLACK,
|
|
|
|
)
|
|
|
|
expected = os.path.join(IMAGES_PATH, "discontiguous_corners_polygon.png")
|
|
|
|
assert_image_similar_tofile(img, expected, 1)
|
2022-05-16 15:31:49 +03:00
|
|
|
|
2022-05-28 08:25:19 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_polygon2() -> None:
|
2022-05-16 15:31:49 +03:00
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
draw.polygon([(18, 30), (19, 31), (18, 30), (85, 30), (60, 72)], "red")
|
|
|
|
expected = "Tests/images/imagedraw_outline_polygon_RGB.png"
|
|
|
|
assert_image_similar_tofile(im, expected, 1)
|
2023-03-01 03:22:35 +03:00
|
|
|
|
|
|
|
|
2023-03-01 14:06:40 +03:00
|
|
|
@pytest.mark.parametrize("xy", ((1, 1, 0, 1), (1, 1, 1, 0)))
|
2024-01-31 13:55:32 +03:00
|
|
|
def test_incorrectly_ordered_coordinates(xy: tuple[int, int, int, int]) -> None:
|
2023-03-01 03:22:35 +03:00
|
|
|
im = Image.new("RGB", (W, H))
|
|
|
|
draw = ImageDraw.Draw(im)
|
|
|
|
with pytest.raises(ValueError):
|
2023-03-01 14:06:40 +03:00
|
|
|
draw.arc(xy, 10, 260)
|
2023-03-01 03:22:35 +03:00
|
|
|
with pytest.raises(ValueError):
|
2023-03-01 14:06:40 +03:00
|
|
|
draw.chord(xy, 10, 260)
|
2023-03-01 03:22:35 +03:00
|
|
|
with pytest.raises(ValueError):
|
2023-03-01 14:06:40 +03:00
|
|
|
draw.ellipse(xy)
|
2023-03-01 03:22:35 +03:00
|
|
|
with pytest.raises(ValueError):
|
2023-03-01 14:06:40 +03:00
|
|
|
draw.pieslice(xy, 10, 260)
|
2023-03-01 03:22:35 +03:00
|
|
|
with pytest.raises(ValueError):
|
2023-03-01 14:06:40 +03:00
|
|
|
draw.rectangle(xy)
|
2023-03-01 03:22:35 +03:00
|
|
|
with pytest.raises(ValueError):
|
2023-03-01 14:06:40 +03:00
|
|
|
draw.rounded_rectangle(xy)
|
2024-06-10 04:50:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_getdraw():
|
|
|
|
with pytest.warns(DeprecationWarning):
|
|
|
|
ImageDraw.getdraw(None, [])
|