+ 1
Can anyone answer my question on Stackoverflow?
Sorry but I feel lazy and I already wrote it all in there with all that fancy markdown, anyway here's the link: https://stackoverflow.com/questions/70560234/JUMP_LINK__&&__python__&&__JUMP_LINK-how-to-replace-several-rgba-colours-with-pil
3 Answers
0
Did you notice that "kuro.png" will change the colors in "new_kuro.png", so normally, this code will overwrite the file (if you try to modify red to blue, then cyan to pink, the final result will be that only cyan will change)
+ 1
you gave here the definition of the function which is pretty good but may you give me how you called it ? I think I know what's the problem
- 1
```
def change_colour(pic, old, new):
im = Image.open(pic)
im = im.convert('RGBA')
data = np.array(im)
red, green, blue, alpha = data.T
selected_areas = (red == old[0]) & (blue == old[1]) & (green == old[2])
data[..., :-1][selected_areas.T] = new
im2 = Image.fromarray(data)
im2.save(f'new_{pic}')
list1 = [(33, 33, 33, 255), (103, 58, 183, 255), (255, 193, 7, 255)]
list2 = [(165, 1, 75, 255), (3, 169, 244, 255), (76, 175, 80, 255)]
change_colour('kuro.png', list1[0], list2[0]) # I can change the (33, 33, 33, 255) in the img for any colour I want
change_colour('kuro.png', list1[1], list2[1]) # But I cannot change (103, 58, 183, 255) for anything
change_colour('kuro.png', list1[2], list2[2]) # Nor (255, 193, 7, 255)
```