问题
import cv2
import numpy as np
img = cv2.imread('C:\Users\SHUSHMA-PC\Desktop\sample\dumb.jpeg',cv2.IMREAD_COLOR)
img[100,100]=[255,255,255]
px=img[100,100]
img[500:550,500:550]=[0,0,0]
dumb_face= img[37:111,108:195]
img[0:74,0:87]=dumb_face
imS = cv2.resize(img, (250, 250))
cv2.imshow("output", imS)
cv2.waitKey()
cv2.destroyAllWindows()
There is no error, it is just that the image is not copying and appears as normal output
Actually, I want to copy the entire image and paste it on the image itself(smaller one). Like image on a image.
回答1:
I think you are trying to crop the image and save the cropped portion as a new image. Below is the sample code to do that.
import cv2
import numpy as np
img = cv2.imread(r'.\dump.png', 1) # '1' read as color image
h,w = img.shape[:2]
print 'image height and width = %d x %d' % (h, w) # 318 * 348 pixels
img = img[50:250,50:280] # crop image at [h1:h2, w1:w2]
cv2.imwrite(r'.\dump_resized.png',img)
Here is cropped and saved image.
This is what you intended to do?
UPDATE:
To resize the image and put it as a picuture-in-picture one. You may resize the image first, by 1/10 for instance.
resized_image = cv2.resize(img, (h/8, w/8))
h1, w1 = resized_image.shape[:2]
Then put the resized one into the original image.
#set top left position of the resized image
pip_h = 10
pip_w = 10
img[pip_h:pip_h+h1,pip_w:pip_w+w1] = resized_image # make it PIP
cv2.imwrite(r'.\dump_pip.png',img)
Here is the resulted PIP image.
来源:https://stackoverflow.com/questions/46617801/how-to-copy-and-paste-of-image-as-picture-in-picture-in-opencv