#!/usr/bin/python3 from PIL import Image im = Image.open("bodensee-m3.png") print(im.format, im.size, im.mode) region_colors = { ( 0, 0, 255, 255), ( 0, 255, 255, 255), ( 0, 255, 0, 255), (255, 255, 0, 255), (255, 0, 0, 255), } seeds = {c: [] for c in region_colors} for y in range(0, im.size[1]): for x in range(0, im.size[0]): p = im.getpixel((x, y)) if p in region_colors: seeds[p].append((x, y)) for y in range(0, im.size[1]): print(f"{y}") for x in range(0, im.size[0]): p = im.getpixel((x, y)) if p[3] == 0: best_color = (0, 0, 0, 255) min_d2 = 10_000_000 for color, points in seeds.items(): for p in points: dx = p[0] - x dy = p[1] - y d2 = dx * dx + dy * dy if d2 < min_d2: best_color = color min_d2 = d2 best_color = (*best_color[:3], 127) im.putpixel((x, y), best_color) im.save("bodensee-r3.png")