1

I was wondering how to change the alpha value of an image (.png or .jpg), only for multiple colours? I have done a some looking around and found some solutions with the PIL module, and I found a partial solution here:

Python: PIL replace a single RGBA color (Code used from link provided)

import Image
import numpy
im = Image.open('test.png').convert('RGBA')
data = numpy.array(im)
r, g, b, a = data.T

colour_to_keep = (r == 255) & (b == 255) & (g == 255) 
data[..., :-1][colour_to_keep.T] = (255, 0, 0)
im2 = Image.fromarray(data)
im2.show()

But after playing around with it, I don't think that it will do what I am attempting to do. This code will replace all except one colour.

I am attempting to set only one "background colour" (in this case, green) of a very simple image to transparent (with alpha = 0).

Can anyone please point me in a direction of how to do this? Thank you!

0 Answers0