from PIL import Image, ImageOps
import numpy as np
import cv2
image='image.png'
img = Image.open(image)
im = ImageOps.grayscale(img)
bgr = cv2.imread(image, cv2.IMREAD_GRAYSCALE)
cv2.imshow('img',bgr)
cv2.waitKey(0)
colour={}
for pixel in im.getdata():
if pixel in colour:
colour[pixel] += 1
else:
colour[pixel] = 1
if len(colour) > 2:
aux = sorted([(v,k) for (k,v) in colour.items()])
res = [(v,k) for (k,v) in aux[-3:]]
print(res,res[0][0],res[2][0])
find = res[0][0] # какие значения заменять
replace = res[2][0] # на какие значения заменять
mask = (bgr == find)[..., 0]
bgr[mask] = replace
cv2.imshow('img',bgr)
cv2.waitKey(0)