Skip to content

Instantly share code, notes, and snippets.

@udf
Created April 13, 2018 13:47
Show Gist options
  • Save udf/c452669e251f8f5507555e3b7826a432 to your computer and use it in GitHub Desktop.
Save udf/c452669e251f8f5507555e3b7826a432 to your computer and use it in GitHub Desktop.
Generates fancy colours by abusing the order(lessness) of a set; it's called ass because i didn't expect it to work so well (or at all)
from PIL import Image
import random
def thing(val):
val += (-4, -3, -2, -1, 1, 2, 3, 4)[random.getrandbits(3)]
if val < 0: return 0
if val > 255: return 255
return val
def neighbours(coord, w, h):
offsets = ((-1, -1), (-1, 0), (-1, 1), (1, -1), (1, 0), (1, 1), (0, -1), (0, 1))
for xo, yo in offsets:
nx = coord[0] + xo
ny = coord[1] + yo
if nx >= 0 and nx < w and ny >= 0 and ny < h:
yield nx, ny
w = 720
h = 1280
im = Image.new('RGB', (w, h))
save_frames = False
done = set()
total = w * h
pending = set()
pending.add((0, 0, 0, (0, 0)))
n_iters = 0
frame = 0
while pending:
if n_iters % 10000 == 0:
print(len(done), total, frame)
if save_frames:
im.save(f'{frame:04d}.bmp')
frame += 1
r, g, b, dest = pending.pop()
r = thing(r)
g = thing(g)
b = thing(b)
im.putpixel(dest, (r, g, b))
done.add(dest)
n_iters += 1
for neighbour in neighbours(dest, w, h):
if neighbour not in done:
pending.add((r, g, b, neighbour))
if save_frames:
im.save(f'{frame:04d}.bmp')
else:
im.save('ass.png')
@Qwerty-Space
Copy link

Awoo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment