Let say that I have a 2 pictures that are transparent. How can I overlay picture 1 to picture 2 so to get a result picture. Picture 1 is also smaller that picture 2. I assume that it can be done with opencv or PIL (GIMP is not allowed)
And picture number 2 :
[
That's the result that I want to get:
My approach : I thought that I can be done with opencv function cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])
what I also tried to perform.
But as the matter in fact major problems are next points:
pictures are transparent and the task is to leave pictures with higher resolution.
Rely on your answers.
CodePudding user response:
As a matter in fact, I find out the solution that suits for me. Perhaps It's not an optimal solution but at least It works.
The main idea is that I merge(paste) 2 pictures into 1 result using PIL library and the method paste
, so that I have background and foreground.
In my case I have Picture 1 - foreground and Picture 2 - background.
My code :
# This function fit picture 2(background) for picture 1 . It's pare
def transform (template):
img = Image.open(template)
img_w, img_h = img.size
image = img.resize((img_w coef_w,img_h coef_h),Image.ANTIALIAS)
# change coef_w and coef_h to resize to fit template
image.save('updated_picture.png',"PNG")
# This function merge,compose, concat) pictures
def compose (image, template):
img = Image.open(image)
img_w, img_h = img.size
print(img.size)
background = Image.open(template)
print(background.size)
# resize the image
size = background.size
background = background.resize(size, Image.ANTIALIAS)
bg_w, bg_h = background.size
offset = ((bg_w - img_w) // 2, (bg_h - img_h) // 2)
background.paste(img, offset, mask=img)
background.save(f'{image}_final.png', "PNG")
How it's work, I transform template(ears) so they would lay on the picture at center and the next one script offset foreground by center so I can get next result.
Transform is needed to make template smaller or bigger. The results would depend on of it's template. I mean the scale of ears in my case. At the next picture you can see what I mean