Skip to content

Instantly share code, notes, and snippets.

@andersource
Created April 3, 2024 20:02
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 andersource/5dec1fafee09c22ae673787b66d8e7e5 to your computer and use it in GitHub Desktop.
Save andersource/5dec1fafee09c22ae673787b66d8e7e5 to your computer and use it in GitHub Desktop.
Code used to generate the Python Turtle Bingo background, inspired by pottery painted using a color "splash"
from tqdm import tqdm
import numpy as np
from skimage.io import imsave
MAX_TILE_SIZE = 120
W, H = 1840, 4200
BACKGROUND = np.array([252, 250, 255], dtype=np.uint8)
SPLASH = np.array([60, 67, 117], dtype=np.uint8)
N = 10000
TILE_W = max(x for x in range(1, MAX_TILE_SIZE + 1) if W % x == 0)
TILE_H = max(x for x in range(1, MAX_TILE_SIZE + 1) if H % x == 0)
THRESHOLD = 1.3
def main():
print(TILE_W, TILE_H)
x_tiles = W // TILE_W
y_tiles = H // TILE_H
res = np.tile(BACKGROUND, (H, W, 1))
splash_x = np.random.randint(W, size=N)
splash_y = np.random.randint(H, size=N)
r = 1 + 1 / (.045 + np.random.random(size=N))
pbar = tqdm(desc='Constructing image', total=x_tiles * y_tiles)
for row in range(y_tiles):
ymin = row * TILE_H
ymax = (row + 1) * TILE_H - 1
for col in range(x_tiles):
xmin = col * TILE_W
xmax = (col + 1) * TILE_W - 1
curr_res = np.tile(BACKGROUND, (TILE_H, TILE_W, 1))
x = np.arange(xmin, xmax + 1)
y = np.arange(ymin, ymax + 1)
xx, yy = np.meshgrid(x, y)
d = (
np.sqrt((xx.ravel().reshape((-1, 1)) - splash_x.reshape((1, - 1))) ** 2 + (yy.ravel().reshape((-1, 1)) - splash_y.reshape((1, - 1))) ** 2).reshape((TILE_H, TILE_W, N))
) / r.reshape((1, 1, -1))
mask = np.sqrt((1 / d ** 2).sum(axis=2)) >= THRESHOLD
curr_res[mask] = SPLASH
res[ymin:ymax + 1][:, xmin:xmax + 1] = curr_res
pbar.update(1)
imsave('???', res)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment