Skip to content

Instantly share code, notes, and snippets.

@jsundram
Last active December 15, 2015 22:19
Show Gist options
  • Save jsundram/5332259 to your computer and use it in GitHub Desktop.
Save jsundram/5332259 to your computer and use it in GitHub Desktop.
Ported from my cleanup (https://gist.github.com/jsundram/1671003) of analogpixel's sketch: http://www.analogpixel.org/blog/2011/09/13/msced-148-boxy-lady-take-2/#more-764 About as straighforward a translation of processing into nodebox as possible.
from nodebox.graphics import *
from numpy.random import randint # random.randint is v. slow
import numpy
MINS, MAXS = 1, 40
grow = False
box_size = MAXS
counter = 1
# Use a numpy array of pixels because pic.get is too slow.
pic = Pixels(Image('pic.jpg'))
pixels = numpy.empty((pic.width, pic.height), dtype=Color)
for x in range(0, pic.width):
for y in range(0, pic.height):
pixels[x][y] = pic.get(x, y)
def setup():
canvas.fps = 30 # pipe dream, each frame takes a while.
canvas.size = pic.size
nostroke()
canvas.run(draw)
def draw(canvas):
global box_size, grow
make(box_size)
box_size += 1 if grow else -1
if box_size <= MINS:
grow = True
elif MAXS <= box_size:
grow = False
canvas.stop()
def make(i):
global counter, pixels
for cc in xrange(pic.width * pic.height / 2):
x = randint(0, pic.width)
y = randint(0, pic.height)
rect(x, y, randint(MINS, i+1), randint(MINS, i+1), fill=pixels[x][y])
# Would love to use canvas.frame, but it doesn't advance properly
canvas.save('nb/image%03d.png' % counter)
print canvas.profiler.framerate
counter += 1
if __name__ == '__main__':
setup()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment