Skip to content

Instantly share code, notes, and snippets.

@gsoykan
Last active October 26, 2022 15:42
Show Gist options
  • Save gsoykan/035caa48188b13be9e2b1233a8692bc2 to your computer and use it in GitHub Desktop.
Save gsoykan/035caa48188b13be9e2b1233a8692bc2 to your computer and use it in GitHub Desktop.
shows an image with a bounding box
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
# source: https://stackoverflow.com/a/37437395/8265079
x_0, y_0, x_1, y_1 = annotation_item['bounding_box']
im = Image.open('my_img.png')
# Create figure and axes
fig, ax = plt.subplots()
# Display the image
ax.imshow(im)
# Create a Rectangle patch
rectangle = patches.Rectangle((x_0, y_0), x_1 - x_0, y_1 - y_0, linewidth=2, edgecolor='r', facecolor='none')
# Add the patch to the Axes
ax.add_patch(rectangle)
rx, ry = rectangle.get_xy()
cx = rx + rectangle.get_width()/2.0
cy = ry + rectangle.get_height()/2.0
ax.annotate("Rectangle", (cx, cy), color='black', weight='bold', fontsize=10, ha='center', va='center')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment