Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

draw bounding box matplotlib

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()
Comment

bounding box in matplotlib

import matplotlib
matplotlib.rc('text',usetex=True)
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
import numpy as np

text = 'egin{tabular}{|c|c|}hline1&2\hline3&4\hlineend{tabular}'

fig, ax = plt.subplots(1)

img = ax.imshow(np.zeros((10,10)), cmap=plt.cm.gray)
txt = ax.text( 4.5,
          4.5,
          text,
          fontsize=24,
          ha='center',
          va='center',
          bbox=dict(alpha=0))

fig.canvas.draw()
bbox = txt.get_bbox_patch()
xmin = bbox.get_window_extent().xmin
xmax = bbox.get_window_extent().xmax
ymin = bbox.get_window_extent().ymin
ymax = bbox.get_window_extent().ymax

xmin, ymin = fig.transFigure.inverted().transform((xmin, ymin))
xmax, ymax = fig.transFigure.inverted().transform((xmax, ymax))

dx = xmax-xmin
dy = ymax-ymin

# The bounding box vals can be tweaked manually here.
rect = Rectangle((xmin-0.02,ymin-0.01), dx+0.04, dy+0.05, fc='w', transform=fig.transFigure)

ax.add_patch(rect)
fig.canvas.draw()
ax.axis('off')
plt.savefig('ok.png',bbox_inches='tight')




Comment

PREVIOUS NEXT
Code Example
Python :: padding figures in pyplot 
Python :: remove a columns in pandas 
Python :: add element to list python 
Python :: pyton for 
Python :: Python communication with serial port 
Python :: split long list into chunks of 100 
Python :: how to add element to list value in a dict python 
Python :: stdin and stdout in python 
Python :: boolean python example 
Python :: python re 
Python :: prime numbers 1 to 100 
Python :: formatting strings in python 
Python :: np.vstack python 
Python :: How to select element using xpath in python 
Python :: how to add items in list in python 
Python :: takes 2 positional arguments but 3 were given 
Python :: show columns with nan pandas 
Python :: joining two lists in python using for loop 
Python :: np.random.randint 
Python :: new line 
Python :: python elif 
Python :: naive bayes implementation in python 
Python :: activate venv in python 
Python :: django model queries 
Python :: open chrome console in selenium 
Python :: how to use underscore in python 
Python :: == in python 
Python :: three different randomn numbers python 
Python :: TypeError: view must be a callable or a list/tuple in the case of include(). 
Python :: how to block a ip adress 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =