Home > Enterprise >  Overlay smaller image to larger image in Python
Overlay smaller image to larger image in Python

Time:03-28

I know this question has been asked for several times, e.g., enter image description here ... larger image is like: enter image description here (The image above has only 1 pixel filled with white color and 90% transparency)

Using the code examples given in the previous question (enter image description here

My desired result is to maintain the larger image's transparency.

May I know how to do so?

CodePudding user response:

This is actually the correct output. Fully transparent images (like your template image) have the pixel value of 0 on empty areas which represents black. If you were to use semi-transparent templates (which have pixels value greater than 0 even if they are slightly transparent). You should see this:

enter image description here

Answer 2:

Your output images are 24bit. (Which means you get rid of thhe alpha channel before saving it). I looked into your code and saw the lines 34 and 35;

img_result = img[:, :, :3].copy()   
img_overlay = img_overlay_rgba[:, :, :3]

You're sending RGB images to overlay_image_alpha function. Change this to;

img_result = img.copy()
img_overlay = img_overlay_rgba

To preserve Alpha channel information.

New Output:

enter image description here

On Photoshop:

enter image description here

  • Related