matplotlib plot rectangle on image code example
Example 1: python draw rectangle on image
img = matplotlib. image. imread("./kite_logo.png")
figure, ax = pyplot. subplots(1)
rect = patches. Rectangle((125,100),50,25, edgecolor='r', facecolor="none")
ax. imshow(img) Displays an image.
ax. add_patch(rect) Add rectangle to image.
Example 2: pyplot rectangle over image
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
im = Image.open('stinkbug.png')
fig, ax = plt.subplots()
ax.imshow(im)
rect = patches.Rectangle((50, 100), 40, 30, linewidth=1, edgecolor='r', facecolor='none')
ax.add_patch(rect)
plt.show()