Skip to content

Instantly share code, notes, and snippets.

Last active December 17, 2015 13:38
Show Gist options
  • Save jsundram/5618041 to your computer and use it in GitHub Desktop.
Save jsundram/5618041 to your computer and use it in GitHub Desktop.
implementation of BoxyLady2 aimed at pypy

To get pypy installed:

  • curl -O
  • tar -xvf pypy-2.0.1-osx64.bz2
  • cd ./pypy-2.0.1/bin
  • curl -O
  • curl -O
  • ./pypy
  • ./pypy
  • export PIP_REQUIRE_VIRTUALENV=false; ./pypy
  • ./pip install hg+
  • ./pip install git+
  • replace sys.getrefcount in nodebox/graphics/ to work with pypy
      from sys import getrefcount
  except ImportError: #pypy
      import gc
      getrefcount = lambda x: len(gc.get_referrers(x)) - 1
from import *
from random import randint
import os
MINS, MAXS = 1, 40
grow = False
box_size = MAXS
counter = 1
output_dir = '/Users/%s/Downloads/nb/' % os.getlogin()
# Use a flat array of pixels because pic.get is too slow.
pic = Pixels(Image('pic.jpg'))
pixels = [pic.get(x,y) for y in range(0, pic.height) for x in range(0, pic.width)]
def setup():
canvas.fps = 2
canvas.size = pic.size
def draw(canvas):
global box_size, grow
box_size += 1 if grow else -1
if box_size <= MINS:
grow = True
elif MAXS <= box_size:
grow = False
def make(i):
global counter, pixels
w, h = pic.width, pic.height
for cc in xrange(w * h / 2):
x = randint(0, w-1)
y = randint(0, h-1)
a = randint(MINS, i)
b = randint(MINS, i)
rect(x, y, a, b, fill=pixels[w*y + x])
# Would love to use canvas.frame, but it doesn't advance properly
# WTF? removing this save() call makes the code ~10x SLOWER??? + 'image%03d.png' % counter)
print canvas.profiler.framerate
counter += 1
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment