Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw Rectangle image in python #182

Open
imsanjoykb opened this issue Feb 22, 2022 · 1 comment
Open

Draw Rectangle image in python #182

imsanjoykb opened this issue Feb 22, 2022 · 1 comment

Comments

@imsanjoykb
Copy link
Owner

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()
@2010030167
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants