Bitwise operation

hello every one
I want to extract one piece of a shape in a picture.
the solution for this is to make a black image and white rectangle in black image.
then the mask image and original image get in and operator to get out the piece of image.
but I get get Error “cv2.bitwise_and(img, blk_copy)”.

my code:
np_img = np.array(img)
black = np.zeros(img.shape)
white = np.ones(img.shape)
blk_copy = black.copy()
cv2.rectangle(blk_copy , (301 , 350) , (60, 110) , (255,255,255) , -1)
new_img = cv2.bitwise_and(img, img, mask = blk_copy)

cv2.imshow(“rec” , blk_copy)
cv2.imshow(“image” , new_img)
cv2.waitKey(0)

can anyone help me.
thanks.

what error specifically? why don’t you say?

cv2.error: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\core\src\arithm.cpp:230: error: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.sameSize(*psrc1) in function 'cv::binary_op

this will produce int32 arrays ! you have to specify the dtype explicitly:

black = np.zeros(img.shape, img.dtype)

and it will (probably) produce 3-dimensional, 3-channel arrays (img = imread()?). that is unsuitable as a mask.