简体   繁体   中英

Resize image in OpenCv python, filling space with color

i'm trying to resize an image to a default value, filling the entire space. I've tried to create a blank background, pasting the image i have but i'm having errors:

# image_toresize it's the image I want to apply over the background
# the image im using for the background
blank_image = np.zeros((600,900,3), np.uint8)
blank_image = (255,255,255)

l_img = blank_image.copy()
x_offset = y_offset = 0
height, width = image_toresize.shape[:2]

l_img[0:height, 0:width] = image_toresize.copy()

this error

ValueError: could not broadcast input array from shape (90,657) into shape (90,657,3)

What can i do?

Try below code:

image_toresize = cv2.imread('flower5.jpg')     
height, width = image_toresize.shape[:2]

blank_image = np.zeros((600,900,3), np.uint8)
blank_image[:,:] = (255,255,255)

l_img = blank_image.copy()                    # (600, 900, 3)

x_offset = y_offset = 100
# Here, y_offset+height <= blank_image.shape[0] and x_offset+width <= blank_image.shape[1]
l_img[y_offset:y_offset+height, x_offset:x_offset+width] = image_toresize.copy()

cv2.imshow('img', l_img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

Figure 1 : Original Image

在此处输入图片说明

Figure 2 : Above image added to a white empty background

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM