def main(filename="output.png", img_width=2000, n=10, max_subdiv=5, palette=random.choice(palettes.PALETTES)): img_height = img_width # Work only with square images ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, img_width, img_height) ims.set_fallback_resolution(300.0, 300.0) ctx = cairo.Context(ims) ctx.rectangle(0, 0, img_width, img_height) ctx.set_source_rgb(*colors.hex_to_tuple(palette['background'])) ctx.fill() size = img_width // n for r in range(0, img_height, size): for c in range(0, img_width, size): subdiv = random.randint(1, max_subdiv) step = size // subdiv for y in range(subdiv): for x in range(subdiv): circle(ctx, c + x * step, r + y * step, step, palette['colors']) ims.write_to_png(filename)
def main(filename="output.png", img_width=2000, n=10, shake_count=5, palette=random.choice(palettes.PALETTES), shakiness=8, line_width=5): img_height = img_width # Work only with square images ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, img_width, img_height) ims.set_fallback_resolution(300.0, 300.0) ctx = cairo.Context(ims) ctx.rectangle(0, 0, img_width, img_height) ctx.set_source_rgb(*colors.hex_to_tuple(palette['background'])) ctx.fill() frame = 20 # empty space around image border size = (img_width - 2 * frame) / n for r in range(n): for c in range(n): tile(ctx, frame + c * size, frame + r * size, size, palette['colors'], count=shake_count, shakiness=shakiness, line_width=line_width) ims.write_to_png(filename)
def circle(ctx, x, y, diameter, cols): x_fuzz = random.uniform(0, 3) * random.choice([-1, 1]) y_fuzz = random.uniform(0, 3) * random.choice([-1, 1]) radius = random.uniform(diameter / 3, diameter / 2) - max( abs(x_fuzz), abs(y_fuzz)) ctx.arc(x + diameter / 2 + x_fuzz, y + diameter / 2 + y_fuzz, radius, 0, 2 * math.pi) ctx.set_source_rgb(*colors.hex_to_tuple(random.choice(cols))) ctx.fill()
def triangle(ctx, p1, p2, p3, cols): chosen_cols = [colors.hex_to_tuple(c) for c in random.sample(cols, 2)] draw_triangle(ctx, p1, p2, p3, chosen_cols[0]) # Inner triangle points = [p1, p2, p3] inner_points = [ point_on_line(p, points[idx - 1]) for (idx, p) in enumerate(points) ] draw_triangle(ctx, *inner_points, chosen_cols[1], outline=False)
def tile(ctx, x, y, size, cols, count=4): shake_factor = (size / 2) / 8 radius = (size / 2) - shake_factor for _ in range(count): x_shake = random.uniform(-1 * shake_factor, shake_factor) y_shake = random.uniform(-1 * shake_factor, shake_factor) ctx.arc(x + size / 2 + x_shake, y + size / 2 + y_shake, radius, 0, 2 * math.pi) color = colors.hex_to_tuple(random.choice(cols)) ctx.set_source_rgb(*color) ctx.set_line_width(max(shake_factor / 5, 1)) ctx.stroke()
def tile(ctx, x, y, size, cols): # Petal lm = (x - size / 2, y + size / 2) tm = (x + size / 2, y) rm = (x + size + size / 2, y + size / 2) bm = (x + size / 2, y + size) for offset in [size / 2, size / 4, 0, -size / 4]: lm = (x - offset, y + size / 2) rm = (x + size + offset, y + size / 2) ctx.curve_to(*bm, *lm, *tm) ctx.curve_to(*rm, *bm, *bm) ctx.set_source_rgb(*colors.hex_to_tuple(random.choice(cols))) ctx.fill()
def main(filename="output.png", img_width=2000, n=10, palette=random.choice(palettes.PALETTES)): img_height = img_width # Work only with square images ims = cairo.ImageSurface(cairo.FORMAT_ARGB32, img_width, img_height) ims.set_fallback_resolution(300.0, 300.0) ctx = cairo.Context(ims) ctx.rectangle(0, 0, img_width, img_height) ctx.set_source_rgb(*colors.hex_to_tuple(palette['background'])) ctx.fill() size = (img_width) / n for r in range(n): for c in range(n + 1): tile_fn = tile_A if r % 2 == 0 else tile_B tile_fn(ctx, c * size, r * size, size, palette['colors']) ims.write_to_png(filename)
def tile(ctx, x, y, size, cols, count=4, shakiness=8, line_width=3): shake_factor = (size / 2) / shakiness x = x + shake_factor + 1 y = y + shake_factor + 1 size = size - 2 * shake_factor - 2 def shake(): return random.uniform(-1 * shake_factor, shake_factor) for _ in range(count - 1): start = (x + shake(), y + shake()) ctx.move_to(*start) ctx.line_to(x + size + shake(), y + shake()) ctx.line_to(x + size + shake(), y + size + shake()) ctx.line_to(x + shake(), y + size + shake()) ctx.line_to(*start) color = colors.hex_to_tuple(random.choice(cols)) ctx.set_source_rgb(*color) ctx.set_line_width(line_width) ctx.stroke()
def triangle(ctx, p1, p2, p3, cols): ctx.move_to(*p1) ctx.line_to(*p2) ctx.line_to(*p3) ctx.set_source_rgb(*colors.hex_to_tuple(random.choice(cols))) ctx.fill()