I'm running the code below to remove unwanted gradients between uniform color patches. This works well enough in plenty of cases however sometimes it fails because of the similarity of some gradient colors and the palette colors, like in the image below (top = original, bottom = result, right = palette image). How could I avoid that?
from PIL import Image
import os
path_images = "D:/RasterCalc/Samples_Original"
path_palette = "D:/RasterCalc/Palettes/palette_nsph.png"
path_output = "D:/RasterCalc/Samples_Converted"
image_palette = (Image.open(path_palette)).convert('RGBA')
image_palette = image_palette.convert('P')
for filename in os.listdir(path_images):
image = (Image.open(path_images+"/"+filename)).convert('RGB')
print(filename)
image_converted = image.quantize(palette=image_palette)
image_converted.save(path_output+"/"+filename)
print("==================")
print("done!")
/edit: now with some circles for clarification!