I am trying to create a simple voronoi image based on another stackoverflow question I've seen: Create Voronoi art with rounded region edges
In the first example the user provides, he is "painting manually" each pixel based on the minimum distance to each centroid (each associated with a color). I have tried replicating his code but am having some trouble.
Voronoi.py:
import random
import numpy as np
import itertools as it
import matplotlib.pyplot as plt
n_centroids = 10
h = 5
w = 5
x = 5
y = 5
img = [x,y]
centroids = [(random.randint(0, h), random.randint(0, w)) for _ in range(n_centroids)]
colors = np.array([np.random.choice(range(256), size=3) for _ in range(n_centroids)]) / 255
for x, y in it.product(range(h), range(w)):
distances = np.sqrt([(x - c[0])**2 + (y - c[1])**2 for c in centroids])
centroid_i = np.argmin(distances)
img[x,y] = colors[centroid_i]
plt.imshow(img, cmap='gray')
plt.show()
When I try to assign img[x,y] = colors[centroid_i]
, I keep getting this error:
TypeError: list indices must be integers or slices, not tuple
I believe it is due to how I am declaring img but cannot quite figure it out.
This is what the end result should look like: