File size: 2,603 Bytes
e97f848
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from pathlib import Path

from PIL import Image, ImageDraw

from .corners import Circle, Corners

DEFAULT_COLOR = (0, 255, 255, 255)


def draw_circle(draw: ImageDraw.Draw, circle: Circle, color: tuple, width: int = 3):
    x1 = circle.x - circle.r
    y1 = circle.y - circle.r
    x2 = circle.x + circle.r
    y2 = circle.y + circle.r
    draw.ellipse([x1, y1, x2, y2], outline=color, width=width)


def draw_circles(
    image: Image.Image,
    corners: Corners,
    color: tuple = DEFAULT_COLOR,
    width: int = 3,
) -> Image.Image:
    image = image.convert("RGBA")
    overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)

    for corner in [corners.top_left, corners.top_right, corners.bottom_left, corners.bottom_right]:
        draw_circle(draw, corner, color, width)

    result = Image.alpha_composite(image, overlay)
    return result.convert("RGB")


def draw_crop_lines(
    image: Image.Image,
    corners: Corners,
    color: tuple = DEFAULT_COLOR,
    width: int = 2,
) -> Image.Image:
    image = image.convert("RGBA")
    overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)

    x_left, y_top, x_right, y_bottom = corners.get_crop_bounds()
    img_w, img_h = image.size

    draw.line([(x_left, 0), (x_left, img_h)], fill=color, width=width)
    draw.line([(x_right, 0), (x_right, img_h)], fill=color, width=width)
    draw.line([(0, y_top), (img_w, y_top)], fill=color, width=width)
    draw.line([(0, y_bottom), (img_w, y_bottom)], fill=color, width=width)

    result = Image.alpha_composite(image, overlay)
    return result.convert("RGB")


def visualize_corners(
    image: Image.Image,
    corners: Corners,
    output_path: Path | None = None,
    color: tuple = DEFAULT_COLOR,
) -> Image.Image:
    image = image.convert("RGBA")
    overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)

    for corner in [corners.top_left, corners.top_right, corners.bottom_left, corners.bottom_right]:
        draw_circle(draw, corner, color, width=3)

    x_left, y_top, x_right, y_bottom = corners.get_crop_bounds()
    img_w, img_h = image.size

    draw.line([(x_left, 0), (x_left, img_h)], fill=color, width=2)
    draw.line([(x_right, 0), (x_right, img_h)], fill=color, width=2)
    draw.line([(0, y_top), (img_w, y_top)], fill=color, width=2)
    draw.line([(0, y_bottom), (img_w, y_bottom)], fill=color, width=2)

    result = Image.alpha_composite(image, overlay)
    result = result.convert("RGB")

    if output_path:
        result.save(output_path)

    return result