Home > Back-end >  Change pixel colors between two different colors
Change pixel colors between two different colors

Time:03-31

I have this image:

enter image description here

And I would like to change the group of pixels(greem colour) between or in contact with red and yellow pixels, by red color, this way:

enter image description here

Sorry, I don't have any one code, because I don't know how to start this, and I did not find an approach to do this. I think a similar logic related to PIL, like this:

import numpy as np
from PIL import Image

im = Image.open('image.png')
data = np.array(im)

r1, g1, b1 = 255, 255, 255 # Original value
r2, g2, b2 = 0, 0, 0 # Value that we want to replace it with

red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]

im = Image.fromarray(data)

But with a condition.

CodePudding user response:

We may start the solution by using enter image description here

top_black:
enter image description here

both_black:
enter image description here

img:
enter image description here


The above solution is not the most general solution.
There is also an option to find all the green contours, create a mask of the contours perimeter, and analyze the colors of the perimeter of each contour (and fill contours with mixed perimeter colors with red color).

  • Related