#!/usr/bin/python3 import time t0 = time.clock_gettime(time.CLOCK_MONOTONIC) from PIL import Image im = Image.open("bodensee-m3.png") print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0) #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)) print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0) 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) print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0) im.save("bodensee-r3.png") print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0)