Menno Van Dijk
Menno Van Dijk

Reputation: 903

Change pixels transparent background

I have several different images in which I want to change every transparent pixel of the image into a different color.

I've found a way to extract all the RGB values of the transparent pixels of an image as such:

data[data[:,:,3]==0,:3][0]

Now when I try to assign a different array to this pixel it doesn't work:

data[data[:,:,3]==0,:3][0] = np.array([255,0,255])

print(data[data[:,:,3]==0,:3][0])
Out[1]: [0 0 0]

How do I change this pixel?

Upvotes: 2

Views: 407

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207465

Updated Answer

I see from your comment that you need more flexibility than I understood from your question. Try this:

In [85]: im = np.array(Image.open('image.png').convert('RGBA'))

In [86]: for x in np.argwhere(im[:,:,3]==0):
    ...:     im[x[0],x[1],:]=np.random.randint(0,255), np.random.randint(0,255), np.random.randint(0,255), np.random.randint(0,255)

Original Answer

I think you mean this:

In [32]: im = np.array(Image.open('image.png').convert('RGBA'))

In [33]: im
Out[33]: 
array([[[126, 126, 126, 255],        <--- grey pixel
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255]],

       [[126, 126, 126, 255],
        [255,   0,   0,   0],        <--- transparent pixel
        [255,   0,   0,   0],        <--- transparent pixel
        [255,   0,   0,   0],        <--- transparent pixel
        [126, 126, 126, 255]],

       [[126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255]]], dtype=uint8)

In [34]: im[im[:,:,3]==0]=(1,2,3,4)

In [35]: im
Out[35]: 
array([[[126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255]],

       [[126, 126, 126, 255],
        [  1,   2,   3,   4],
        [  1,   2,   3,   4],
        [  1,   2,   3,   4],
        [126, 126, 126, 255]],

       [[126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255],
        [126, 126, 126, 255]]], dtype=uint8)

Sample image:

It's a row of three transparent red pixels surrounded by an opaque grey border one pixel wide. It's quite small...

enter image description here

Upvotes: 1

Menno Van Dijk
Menno Van Dijk

Reputation: 903

While a vectorized operation might be available for what I was trying to achieve, the solution I've found to work goes as follows.

for h in range(0,data.shape[0]):
    for w in range(0,data.shape[1]):
        if data[h, w][3] == 0:
            data[h, w] = np.random.randint(0,255), np.random.randint(0,255), np.random.randint(0,255), 255

This goes over every pixel in the image, then checks whether that pixel is transparant or not and if it is it randomizes the RGB values of that pixel. Might be rather slow but works as intended!

Upvotes: 2

Related Questions