def frosted_glass(img_path, reduced_height=500): image = Image.open(img_path) width, height = image.size resize_height = reduced_height resize_width = round(resize_height * (width / height)) image = image.resize((resize_width, resize_height)) dummy_img = Image.new('RGB', (resize_width, resize_height)) draw = ImageDraw.Draw(dummy_img) num_of_points = round((resize_width * resize_width) / 100) vor = VoronoiDiagram(resize_width, resize_height, num_of_points) vor.optimize() for group in vor.coor_groupings: num_of_coors = max(1, len(group)) avg_red, avg_green, avg_blue = 0, 0, 0 for coor in group: red, green, blue = image.getpixel(coor) avg_red += red avg_green += green avg_blue += blue avg_red /= num_of_coors avg_green /= num_of_coors avg_blue /= num_of_coors new_color = (round(avg_red), round(avg_green), round(avg_blue)) for coor in group: draw.point(coor, new_color) return dummy_img
def generate_map(self, size=75, freq=20, lloyds=2, sigma=3.15, seed=None): """Initializes the map and generates the base noise and voronoi diagram""" # make up data points size_sqrt = size size = size**2 # compute Voronoi tesselation if seed is not None: np.random.seed(seed) points = np.random.random((size, 2)) vor = VoronoiDiagram(points) vor.generate_voronoi() self.voronoi_diagram = vor.relax_points(lloyds) regions, vertices = voronoi_finite_polygons_2d(self.voronoi_diagram) polygons = sorted(clip(points, regions, vertices), key=lambda x: x[0][0]) polygons_stripped = [[ vert for vert in poly if 0 <= vert[0] <= 1 and 0 <= vert[1] <= 1 ] for poly in polygons] self.polygons = [Polygon(i) for i in polygons_stripped] # Get noise, turn into 1D array noise = Perlin(freq, shuffle_seed=seed) noise_img = noise.create_image(save=True, width=200, height=200) noise_gauss = noise.gaussian_kernel(noise_img, nsig=sigma) noise_gauss.save("images/noise.png") noise_resized = noise_gauss.resize((size_sqrt, size_sqrt)) noise_arr = np.array(noise_resized) self._generate_base_terrain(noise_arr)
def multi_circles(width, length, num_of_points=random.randint(3, 7), num_of_colors=random.randint(4, 10), optimize=False): vrn = VoronoiDiagram(width, length, num_of_points) if optimize: vrn.optimize() points = vrn.feature_points points_to_corners = [] corners = [(0, 0), (width, length), (width, 0), (0, length)] for corner in corners: min_dist = None for point in points: cur_dist = euclidean_distance(point[0], point[1], corner[0], corner[1]) if min_dist is None: min_dist = cur_dist elif cur_dist < min_dist: min_dist = cur_dist points_to_corners.append(min_dist) max_dist = math.ceil(max(points_to_corners)) colors = RGB.n_random_rbg(num_of_colors) gradient = create_color_gradient(colors, max_dist) img = Image.new('RGB', (width, length), gradient[-1]) draw = ImageDraw.Draw(img) for idx in range(max_dist - 1, -1, -1): cur_color = gradient[idx] for point in points: x = point[0] y = point[1] coors = (x - idx, y - idx, x + idx, y + idx) draw.ellipse(coors, cur_color) return img
import tkinter from voronoi import VoronoiDiagram root = tkinter.Tk() root.state('zoomed') w, h = root.winfo_screenwidth(), root.winfo_screenheight() root.geometry("%dx%d+0+0" % (w, h)) voronoi_diagram = VoronoiDiagram(root, width=w, height=h, borderwidth=0, highlightthickness=0, bg="black") voronoi_diagram.pack() root.update() voronoi_diagram.init_diagram() def task(): voronoi_diagram.delete("all") voronoi_diagram.render() root.after(10, task) # reschedule event in 2 seconds root.after(10, task) root.mainloop()
y_offset = Y_OFFSET + ((x + 1) % 2) * Y_SHIFT points.append( Vector2( x * X_DELTA + gauss(0, X_SD) + x_offset, y * Y_DELTA + gauss(0, Y_SD) + y_offset, )) # Instead place points by uniform random sampling? if False: points = [] for i in range(N_X * N_Y): x = randint(0, 1000) y = randint(0, 900) points.append(Vector2(x, y)) voronoi = VoronoiDiagram(points) start_ix = voronoi.create_maze() def main(): draw_points = False draw_triangulation = False draw_circumcircles1 = False draw_circumcircles2 = False draw_voronoi_cells = False draw_voronoi_connections = True draw_maze_edges = True pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT))