Debian Codegraph @

examples/draw.py

raw symbols SCIP
1 #!/usr/bin/env python
2
3 import matplotlib.pyplot as plt
4
5 import imgviz
6
7
8 def draw():
9 img = imgviz.data.lena()
10 H, W = img.shape[:2]
11 viz = img
12
13 y1, x1 = 200, 180
14 y2, x2 = 400, 380
15 viz = imgviz.draw.rectangle(
16 viz, (y1, x1), (y2, x2), outline=(255, 255, 255), width=5
17 )
18 viz = imgviz.draw.text_in_rectangle(
19 viz,
20 loc="lt",
21 text="face",
22 size=30,
23 background=(255, 255, 255),
24 aabb1=(y1, x1),
25 aabb2=(y2, x2),
26 )
27
28 # eye, eye, nose, mouse, mouse
29 yxs = [(265, 265), (265, 330), (320, 315), (350, 270), (350, 320)]
30
31 # eye segment
32 viz = imgviz.draw.line(viz, yx=[yxs[0], yxs[1]], fill=(255, 255, 255), width=5)
33 # mouse segment
34 viz = imgviz.draw.line(viz, yx=[yxs[3], yxs[4]], fill=(255, 255, 255), width=5)
35
36 colors = imgviz.label_colormap(value=255)[1:]
37 shapes = ["star", "ellipse", "rectangle", "circle", "triangle"]
38 for yx, color, shape in zip(yxs, colors, shapes):
39 size = 20
40 if shape == "star":
41 viz = imgviz.draw.star(
42 viz, center=(yx[0], yx[1]), size=1.2 * size, fill=color
43 )
44 elif shape == "ellipse":
45 viz = imgviz.draw.ellipse(
46 viz,
47 yx1=(yx[0] - 8, yx[1] - 16),
48 yx2=(yx[0] + 8, yx[1] + 16),
49 fill=color,
50 )
51 elif shape == "circle":
52 viz = imgviz.draw.circle(
53 viz, center=(yx[0], yx[1]), diameter=size, fill=color
54 )
55 elif shape == "triangle":
56 viz = imgviz.draw.triangle(
57 viz, center=(yx[0], yx[1]), size=size, fill=color
58 )
59 elif shape == "rectangle":
60 viz = imgviz.draw.rectangle(
61 viz,
62 aabb1=(yx[0] - size / 2, yx[1] - size / 2),
63 aabb2=(yx[0] + size / 2, yx[1] + size / 2),
64 fill=color,
65 )
66 else:
67 raise ValueError("unsupport shape: {}".format(shape))
68
69 img = imgviz.draw.text_in_rectangle(
70 img,
71 loc="lt+",
72 text="original",
73 size=30,
74 background=(255, 255, 255),
75 )
76 viz = imgviz.draw.text_in_rectangle(
77 viz,
78 loc="lt+",
79 text="markers",
80 size=30,
81 background=(255, 255, 255),
82 )
83
84 # -------------------------------------------------------------------------
85
86 plt.figure(dpi=200)
87
88 plt.subplot(121)
89 plt.title("original")
90 plt.imshow(img)
91 plt.axis("off")
92
93 plt.subplot(122)
94 plt.title("markers")
95 plt.imshow(viz)
96 plt.axis("off")
97
98 img = imgviz.io.pyplot_to_numpy()
99 plt.close()
100
101 return img
102
103
104 if __name__ == "__main__":
105 from base import run_example
106
107 run_example(draw)