num_points = 500 # Number of points for triangulation
# Function to generate random points within the image dimensions
def generate_points(image_width, image_height, num_points):
points = np.zeros((num_points, 2))
for i in range(num_points):
points[i] = [np.random.uniform(0, image_width), np.random.uniform(0, image_height)]
# Delaunator code included directly for Delaunay triangulation
def __init__(self, coords):
self.coords_len = len(coords) // 2
self.hash_size = int(np.ceil(np.sqrt(self.coords_len)))
self.hull_prev = [-1] * self.coords_len
self.hull_next = [-1] * self.coords_len
self.hull_tri = [-1] * self.coords_len
coords_len = self.coords_len
ids = list(range(coords_len))
ids.sort(key=lambda i: (coords[2 * i], coords[2 * i + 1]))
e = self.hull_prev[0] = 0
for i in range(1, coords_len):
if coords[2 * ids[i]] != coords[2 * ids[0]] or coords[2 * ids[i] + 1] != coords[2 * ids[0] + 1]:
for i in range(1, coords_len):
self.hull_next[e] = ids[i - 1]
def _hash_key(self, x, y):
return int(p % self.hash_size)
return self.hash_map[self._hash_key(x, y)]
return np.array(self.triangles).reshape(-1, 3)
def delaunay_triangulation(points):
coords = points.flatten().tolist()
delaunay = Delaunator(coords)
return delaunay.triangles()
image_top = op('moviefilein1') # Replace with your image TOP path
print("Error: The image TOP could not be found. Check the path.")
width, height = image_top.width, image_top.height
points = generate_points(width, height, num_points)
# Perform Delaunay triangulation
triangles = delaunay_triangulation(points)
# Create points and store their indices
point_index = scriptOp.appendPoint()
scriptOp.points[point_index].P = [point[0], point[1], 0]
point_indices.append(point_index)
for triangle in triangles:
scriptOp.appendPolygon([point_indices[triangle[0]], point_indices[triangle[1]], point_indices[triangle[2]]])
# Set point colors based on image color
for i, point in enumerate(points):
x, y = int(point[0]), int(point[1])
r, g, b, a = image_top.sample(x / width, y / height)
scriptOp.points[i].color = [r, g, b]