Newbie Question: How to apply a Pixel by Pixel mask, where the alpha channel is what I need to set? #4570
-
Hello, I am porting a program that uses a Bitmap to instead use an NetVips.Image. the Bitmap does something like:
I have been looking at NetVips.Image.Mutate and NetVips.Combine, and tried to find something similar in the examples but nothing stood out to me for manipulating individual pixels alpha channel. What docs or examples should I focus on to understand how to mask the alpha channel? Is there a better way without masking? Any help is much appreciated :) Thanks much! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi @timeichfeld-msa, You can just use the usual arithmetic operators and they will automatically loop for you. In python, for example: # some RGB image
image = pyvips.Image.new_from_file("xxx.jpg")
# use [] to get bands (channels) from an image ... these number from zero, so band 0 is red
# relational operators return images with 0 for black and 255 for white, so this will
# be opaque for reds over 220
alpha = image[0] > 220
# append the alpha to make a 4 band RGBA image
image = image.bandjoin(alpha)
# which we can save as something like PNG
image.write_to_file("yyy.png") |
Beta Was this translation helpful? Give feedback.
Ah, Got it.
Also, I am working with a BW 2channel PNG image.
I think I was having trouble because because I couldn't actually find the alpha channel.
Doing the following:
condition always returned false - indicating the image had an alpha, but
was not returning the alpha channel.
What got me to a better place was
then working with that.
I will try out the ifthenelse as you suggest, That looks pro…