File: draw.py

package info (click to toggle)
python-imgviz 1.2.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,268 kB
  • sloc: python: 3,032; makefile: 15
file content (94 lines) | stat: -rwxr-xr-x 2,344 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python

import matplotlib.pyplot as plt

import imgviz


def draw():
    img = imgviz.data.lena()
    H, W = img.shape[:2]
    viz = img

    y1, x1 = 200, 180
    y2, x2 = 400, 380
    viz = imgviz.draw.rectangle(
        viz, (y1, x1), (y2, x2), outline=(255, 255, 255), width=5
    )
    viz = imgviz.draw.text_in_rectangle(
        viz,
        loc="lt",
        text="face",
        size=30,
        background=(255, 255, 255),
        aabb1=(y1, x1),
        aabb2=(y2, x2),
    )

    # eye, eye, nose, mouse, mouse
    xys = [(265, 265), (330, 265), (315, 320), (270, 350), (320, 350)]
    colors = imgviz.label_colormap(value=255)[1:]
    shapes = ["star", "star", "rectangle", "circle", "triangle"]
    for xy, color, shape in zip(xys, colors, shapes):
        size = 20
        if shape == "star":
            viz = imgviz.draw.star(
                viz, center=(xy[1], xy[0]), size=1.2 * size, fill=color
            )
        elif shape == "circle":
            viz = imgviz.draw.circle(
                viz, center=(xy[1], xy[0]), diameter=size, fill=color
            )
        elif shape == "triangle":
            viz = imgviz.draw.triangle(
                viz, center=(xy[1], xy[0]), size=size, fill=color
            )
        elif shape == "rectangle":
            viz = imgviz.draw.rectangle(
                viz,
                aabb1=(xy[1] - size / 2, xy[0] - size / 2),
                aabb2=(xy[1] + size / 2, xy[0] + size / 2),
                fill=color,
            )
        else:
            raise ValueError("unsupport shape: {}".format(shape))

    img = imgviz.draw.text_in_rectangle(
        img,
        loc="lt+",
        text="original",
        size=30,
        background=(255, 255, 255),
    )
    viz = imgviz.draw.text_in_rectangle(
        viz,
        loc="lt+",
        text="markers",
        size=30,
        background=(255, 255, 255),
    )

    # -------------------------------------------------------------------------

    plt.figure(dpi=200)

    plt.subplot(121)
    plt.title("original")
    plt.imshow(img)
    plt.axis("off")

    plt.subplot(122)
    plt.title("markers")
    plt.imshow(viz)
    plt.axis("off")

    img = imgviz.io.pyplot_to_numpy()
    plt.close()

    return img


if __name__ == "__main__":
    from base import run_example

    run_example(draw)