matplotlib: how to draw a rectangle on image
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Forest of Spells Looping
--
Chapters
00:00 Question
00:25 Accepted answer (Score 442)
01:08 Answer 2 (Score 57)
01:51 Answer 3 (Score 29)
02:09 Answer 4 (Score 6)
02:52 Thank you
--
Full question
https://stackoverflow.com/questions/3743...
Question links:
[image]: https://i.stack.imgur.com/KWG46.jpg
Accepted answer links:
[Rectangle]: https://matplotlib.org/api/_as_gen/matpl...
[here]: http://matplotlib.org/users/image_tutori...
[image]: https://i.stack.imgur.com/4MJtp.png
Answer 4 links:
[matplotlib]: https://matplotlib.org/
[PIL's ImageDraw]: https://pillow.readthedocs.io/en/4.2.x/r...
[OpenCV]: https://opencv.org/
[PIL's ImageDraw method to draw a rectangle]: https://pillow.readthedocs.io/en/4.2.x/r...
[OpenCV's methods for drawing a rectangle]: https://docs.opencv.org/3.4.1/d6/d6e/gro...
[Draw a rectangle and a text in it using PIL]: https://stackoverflow.com/questions/4140...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #image #matplotlib
#avk47
ACCEPTED ANSWER
Score 498
You can add a Rectangle patch to the matplotlib Axes.
For example (using the image from the tutorial here):
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
im = Image.open('stinkbug.png')
# Create figure and axes
fig, ax = plt.subplots()
# Display the image
ax.imshow(im)
# Create a Rectangle patch
rect = patches.Rectangle((50, 100), 40, 30, linewidth=1, edgecolor='r', facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
plt.show()
ANSWER 2
Score 63
There is no need for subplots, and pyplot can display PIL images, so this can be simplified further:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image
im = Image.open('stinkbug.png')
# Display the image
plt.imshow(im)
# Get the current reference
ax = plt.gca()
# Create a Rectangle patch
rect = Rectangle((50,100),40,30,linewidth=1,edgecolor='r',facecolor='none')
# Add the patch to the Axes
ax.add_patch(rect)
Or, the short version:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from PIL import Image
# Display the image
plt.imshow(Image.open('stinkbug.png'))
# Add the patch to the Axes
plt.gca().add_patch(Rectangle((50,100),40,30,linewidth=1,edgecolor='r',facecolor='none'))
ANSWER 3
Score 30
You need use patches.
import matplotlib.pyplot as plt
import matplotlib.patches as patches
fig2 = plt.figure()
ax2 = fig2.add_subplot(111, aspect='equal')
ax2.add_patch(
patches.Rectangle(
(0.1, 0.1),
0.5,
0.5,
fill=False # remove background
) )
fig2.savefig('rect2.png', dpi=90, bbox_inches='tight')
ANSWER 4
Score 7
From my understanding matplotlib is a plotting library.
If you want to change the image data (e.g. draw a rectangle on an image), you could use PIL's ImageDraw, OpenCV, or something similar.
Here is PIL's ImageDraw method to draw a rectangle.
Here is one of OpenCV's methods for drawing a rectangle.
Your question asked about Matplotlib, but probably should have just asked about drawing a rectangle on an image.
Here is another question which addresses what I think you wanted to know: Draw a rectangle and a text in it using PIL
