pythonnumpyimage-processingalpha-transparency

changing alpha-channel of select-pixels of image using numpy


Relatively new to numpy - trying to use it for image-processing; I have an image in the RGBA color-space; the image is mostly transparent w/ a small-object whose transparency I want to change from 255 to 127, without affecting the transparency of surrounding pixels whose alpha is 0. I can index into the array and find all the groups of pixels I want, but am struggling on how to actually change their alpha-channels to 127 w/o affecting the alpha-channels of all the pixels; here is how I am indexing into the array - the things I've tried either do not affect the array or else reassign all the alphas in the entire array to 127:

>>> from PIL import Image
>>> import numpy as np
>>> a = Image.open('a.png')
>>> b = np.array(a)
>>> b.shape
(1080, 1080, 4)
>>> b[b[:,:,3]==255][:,3]
array([255, 255, 255, ..., 255, 255, 255], dtype=uint8)
>>> b[b[:,:,3]==255][:,3] = 127
>>> b[b[:,:,3]==255][:,3]
array([255, 255, 255, ..., 255, 255, 255], dtype=uint8)

Solution:

>>> b[540,540]
array([  0,   0,   0, 255], dtype=uint8)
>>> alpha = b[:,:,3]
>>> b[alpha==255,3]=127
>>> b[540,540]
array([  0,   0,   0, 127], dtype=uint8)

Solution

  • It's because double slicing the array will produce a view of the copy of the array. That's why your array will not be updated. Instead, you can put it together in one slice.

    b[b[:,:,3]==255,3] = 127