Skip to content

Commit 3828c8d

Browse files
committed
docs(examples): add rotated_rectangle example
1 parent 7086c59 commit 3828c8d

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ tiled = imgviz.tile(
155155
<td><pre><a href="examples/resize.py">examples/resize.py</a></pre></td>
156156
<td><img src="examples/assets/resize.jpg" width="52.21052631578947%" /></td>
157157
</tr>
158+
<tr>
159+
<td><pre><a href="examples/rotated_rectangle.py">examples/rotated_rectangle.py</a></pre></td>
160+
<td><img src="examples/assets/rotated_rectangle.jpg" width="20.0%" /></td>
161+
</tr>
158162
<tr>
159163
<td><pre><a href="examples/rounded_rectangle.py">examples/rounded_rectangle.py</a></pre></td>
160164
<td><img src="examples/assets/rounded_rectangle.jpg" width="20.0%" /></td>
71.6 KB
Loading

examples/rotated_rectangle.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
import matplotlib.pyplot as plt
4+
5+
import imgviz
6+
7+
8+
def rotated_rectangle() -> None:
9+
img = imgviz.data.lena()
10+
height, width = img.shape[:2]
11+
colors = imgviz.label_colormap()[1:]
12+
13+
angles = [0, 30, 60]
14+
box_height = height * 0.5
15+
box_width = width * 0.22
16+
17+
viz = img
18+
for i, angle in enumerate(angles):
19+
cy = height / 2
20+
cx = width * (i + 1) / (len(angles) + 1)
21+
color = colors[i]
22+
viz = imgviz.draw.rotated_rectangle(
23+
viz,
24+
center=(cy, cx),
25+
size=(box_height, box_width),
26+
angle=angle,
27+
outline=(int(color[0]), int(color[1]), int(color[2])),
28+
width=5,
29+
)
30+
viz = imgviz.draw.text_in_rectangle(
31+
viz,
32+
loc="lt+",
33+
text=f"angle={angle}",
34+
size=18,
35+
background=(int(color[0]), int(color[1]), int(color[2])),
36+
yx1=(cy - box_height / 2, cx - box_width / 2),
37+
yx2=(cy + box_height / 2, cx + box_width / 2),
38+
)
39+
40+
plt.figure(dpi=200)
41+
plt.imshow(viz)
42+
plt.axis("off")
43+
44+
45+
if __name__ == "__main__":
46+
from _base import run_example
47+
48+
run_example(rotated_rectangle)

0 commit comments

Comments
 (0)