Data-Science-Regular-Bootcamp icon indicating copy to clipboard operation
Data-Science-Regular-Bootcamp copied to clipboard

Draw Rectangle image in python

Open imsanjoykb opened this issue 3 years ago • 1 comments

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np

x = np.array(Image.open('geek.png'), dtype=np.uint8)
plt.imshow(x)

# Create figure and axes
fig, ax = plt.subplots(1)

# Display the image
ax.imshow(x)

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

imsanjoykb avatar Feb 22 '22 17:02 imsanjoykb

please try this code it will work

import cv2 from google.colab.patches import cv2_imshow

path

path = 'geek.png'

Reading an image in grayscale mode

image = cv2.imread(path, 1)

Window name in which image is displayed

window_name = 'Image'

Start coordinate, here (100, 50)

represents the top left corner of rectangle

start_point = (80, 40)

Ending coordinate, here (125, 80)

represents the bottom right corner of rectangle

end_point = (150, 80)

Black color in BGR

color = (1, 1, 1)

Line thickness of -1 px

Thickness of -1 will fill the entire shape

thickness = -1

Using cv2.rectangle() method

Draw a rectangle of black color of thickness -1 px

image = cv2.rectangle(image, start_point, end_point, color, thickness)

Displaying the image

cv2_imshow(image)

2010030167 avatar Sep 12 '22 03:09 2010030167