akarshkumar0101
akarshkumar0101

Reputation: 367

Python Transparent Image masking

I have two images of equal size. Both of them have shape (h,w,4) which is 4 channels for RGB and Alpha.

I would like to put all of the pixels of img2 onto img1 wherever img2's alpha value is greater than zero (And not translate the transparent parts of img2). How can I achieve this?

I would highly prefer answers that show me how to create a mask for this situation and allows me to simply do: img1[mask] = img2[mask]

Thank you!

Upvotes: 0

Views: 84

Answers (1)

tel
tel

Reputation: 13999

That should be super easy. You can make the mask like so:

mask = img2[..., 3] > 0

Then, like you said, just do:

img1[mask] = img2[mask]

Upvotes: 1

Related Questions