I need to overlay only a part of the image in my work, so I investigated the method, so I will leave it as a memo.
At first I was planning to use opencv and stack it, but while I was investigating, it seemed easier to use Pillow, so I switched to that method.
The images used are as follows
First, the image is transparent. If you search for this, it will come up quite a bit, so I think you can do it right away.
The implementation is as follows.
from PIL import Image
im1 = Image.open("test1.png ")
im2 = Image.open("test2.png ")
im1.putalpha(128)
im2.putalpha(128)
What you are doing
putalpha (alpha)
Next, the transparent image is overlaid. Create an original image when overlaying, and overlay the transparent image as if you were pasting it.
The implementation is as follows.
bg = Image.new("RGBA", (1000, 1000), (255, 255, 255, 0))
bg.paste(im1, (200, 200), im1)
bg.paste(im2, (400, 400), im2)
bg.save("join.png ")
What you are doing
paste ()
to overlay the transparent image on the base image.save ()
.The saved image is below.
You can see that the transparent images are superimposed in a nice way!
Recommended Posts