2024-04-28 20:08:18 +02:00
|
|
|
#!/usr/bin/python3
|
2024-05-03 23:50:23 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
t0 = time.clock_gettime(time.CLOCK_MONOTONIC)
|
|
|
|
|
2024-04-28 20:08:18 +02:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
im = Image.open("bodensee-m3.png")
|
2024-05-03 23:50:23 +02:00
|
|
|
print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0)
|
|
|
|
#print(im.format, im.size, im.mode)
|
2024-04-28 20:08:18 +02:00
|
|
|
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))
|
2024-05-03 23:50:23 +02:00
|
|
|
print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0)
|
2024-04-28 20:08:18 +02:00
|
|
|
for y in range(0, im.size[1]):
|
2024-05-03 23:50:23 +02:00
|
|
|
#print(f"{y}")
|
2024-04-28 20:08:18 +02:00
|
|
|
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)
|
|
|
|
|
2024-05-03 23:50:23 +02:00
|
|
|
print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0)
|
2024-04-28 20:08:18 +02:00
|
|
|
im.save("bodensee-r3.png")
|
2024-05-03 23:50:23 +02:00
|
|
|
print(time.clock_gettime(time.CLOCK_MONOTONIC) - t0)
|