0

I have this image which has 4 channels. What I want to do is, to reduce this image's opacity. I want the image to remain transparent, however, I just want to reduce the opacity of the VW logo part. (Using opencv/numpy and python)

Here's what I tried :

logo = cv2.imread(FILE_PATH, cv2.IMREAD_UNCHANGED)
logo[:, :, 3] = 50

But this assigns the value 50 to all over the image, giving me this result. Notice how the remaining part of the image is no more transparent (I want it to remain like the original one.)

I thought of something like:

#This is my logic NOT ANY CODE, I want to do something like this:

if (any of) other 3 channels are non zero, make alpha channel of that pixel = 50.
else, keep that pixel as it is. (This pixel would be part of logo)

Is there any way of achieving this result, by using opencv / numpy in python? My last option would be to iterate through all the pixels and look for above conditions, but I feel that would be inefficient.

This answer is exactly what I DON'T want. I want only the logo part(Colored pixels)'s alpha channel to be set to 50.

Aline
  • 1
  • logo[(logo[..., :3]!=0).any(2), 3] = 50. For more information see [this post](https://stackoverflow.com/questions/19766757/replacing-numpy-elements-if-condition-is-met) – yann ziselman Aug 04 '21 at 07:19

1 Answers1

1

Just fleshing out the comment from @yann-ziselman...

enter image description here

Here are the RGBA channels of your image side-by-side, with a red border so you can see the extent on Stack Overflow's background:

enter image description here

Here's how to do what you ask:

import cv2

# Load image including alpha channel
im = cv2.imread('logo.png', cv2.IMREAD_UNCHANGED)

# Set alpha channel to 50 anywhere none of the BGR channels is non-zero
im[(im[..., :3]!=0).any(2), 3] = 50

# Save result
cv2.imwrite('result.png', im)

Result

enter image description here

Result split into RGBA channels side-by-side

enter image description here

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432