-2

I have a png image and a background image in jpg format. I want to overlay the png image over the jpg image. The PNG image should completely overlap the Jpg image

When I use this code I get the error message that the images cannot be overlaid because the dimensions of the images do not match. If I adjust the dimensions, then the images are shown above and below, meaning they don't overlap

img1 = cv2.imread('test.png')
img2 = cv2.imread('test1.jpg')
img3 = vis = np.concatenate((img1, img2), axis=0)

How can I solve this problem? I am very thankful for tips.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Interpreter67
  • 57
  • 2
  • 6
  • what do you think `np.concatenate()` does? are you really trying to embed the images? – ti7 May 30 '22 at 22:28
  • Yes, you are right. I thought the np.concatenate() method would embed the images. This function only arranges the two images side by side or on top of each other. I need another function to achieve this – Interpreter67 May 30 '22 at 22:42
  • What are the dimensions in pixels of the two images please? Does the PNG image have any transparency? Where should the PNG image be placed if it is smaller or bigger than the JPEG? – Mark Setchell May 31 '22 at 00:32
  • Does this answer your question? [Using openCV to overlay transparent image onto another image](https://stackoverflow.com/questions/40895785/using-opencv-to-overlay-transparent-image-onto-another-image) – Christoph Rackwitz May 31 '22 at 11:20

1 Answers1

1

Updated answer in opencv. For better understanding, please refer to this tutorial.

import cv2

img1 = cv2.imread('test.png')
img2 = cv2.imread('test1.jpg')

# Resizing into same shape
img1 = cv2.resize(img1, (400, 400))
img2 = cv2.resize(img2, (400, 400))
 
result = cv2.addWeighted(img1, 0.3, img2, 0.7, 0.0);
 
cv2.imshow('blend', result)
 
cv2.waitKey(0)
cv2.destroyAllWindows()
Asadullah Naeem
  • 347
  • 2
  • 11