+ 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

3rd Jan 2022, 2:10 PM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar
3 ответов
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)
4th Jan 2022, 6:38 PM
VCoder
VCoder - avatar
+ 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
3rd Jan 2022, 9:25 PM
VCoder
VCoder - avatar
- 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) ```
4th Jan 2022, 12:31 PM
Grasshopper Hell Monkey
Grasshopper Hell Monkey - avatar