Skip to content

Instantly share code, notes, and snippets.

@Zulko
Created April 26, 2018 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zulko/037edf4435b4c06906b924b0d6bd3f82 to your computer and use it in GitHub Desktop.
Save Zulko/037edf4435b4c06906b924b0d6bd3f82 to your computer and use it in GitHub Desktop.
Draw any picture with plain circles
import gizeh as gz
from PIL import Image
import numpy as np
def draw_with_circles(image_path, min_r=2, max_r=10, skew=0.2,
padding=0.5, bg_color=(1, 1, 1),
draw_bg_color=False,
pdf_path=None, png_path=None):
image = np.array(Image.open(image_path)) / 255.0
height, width, _ = image.shape
canvas = np.ones((height, width)) * max_r
circles = []
while True:
free_xy = list(zip(*(canvas > min_r).nonzero()[::-1]))
if free_xy == []:
break
x, y = xy = free_xy[np.random.randint(0, len(free_xy))]
max_rad = canvas[y, x]
r = min_r + (max_rad - min_r) * np.random.rand()**skew
circles.append((x, y, r, image[y, x]))
xdist = (np.arange(width) - x)
ydist = np.arange(height).reshape((height, 1)) - y
distances = np.sqrt(xdist**2 + ydist**2)
canvas = np.minimum(canvas, distances - r - padding)
if pdf_path:
surface = gz.PDFSurface(pdf_path, width, height, bg_color=bg_color)
for (x, y, r, color) in circles:
gz.circle(r, fill=color, xy=(x, y)).draw(surface)
surface.finish()
if png_path:
surface = gz.Surface(width, height, bg_color=bg_color)
for (x, y, r, color) in circles:
gz.circle(r, fill=color, xy=(x, y)).draw(surface)
surface.write_to_png(png_path)
for i in range(2):
path = './tries/try_%03d' % i
surface = draw_with_circles('./plain_logo.png',
png_path=path + '.png',
pdf_path=path + '.pdf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment