|
from __future__ import division |
|
from nodebox.graphics import * |
|
from numpy.random import randint # random.randint is v. slow |
|
import numpy |
|
import os, sys |
|
import skimage.io |
|
import skimage.transform |
|
|
|
# necessary on windows to prevent pyglet.gl.ContextException: Unable to share contexts? |
|
os.environ['PYGLET_SHADOW_WINDOW']="0" |
|
|
|
points = [] |
|
pic = None |
|
pixels = None |
|
|
|
|
|
def resize_image(filename): |
|
"""Reads filename and resizes to fit screen""" |
|
name, ext = os.path.splitext(filename) |
|
# Did we already do this one? |
|
resized = name + '_resized' + ext |
|
if os.path.exists(resized): |
|
print "using existing resized file:", resized |
|
return resized |
|
|
|
img = skimage.io.imread(filename) |
|
ih, iw, ic = img.shape |
|
sh, sw = canvas.screen.height - 80, canvas.screen.width |
|
|
|
# Are we too small to bother resizing? |
|
if ih < sh and iw < sw: |
|
print "don't need to resize", filename, (ih, iw) |
|
return filename |
|
|
|
# Do the rescale: |
|
print "resizing", filename |
|
scale = min(sw / iw, sh / ih) |
|
dw, dh = int(scale * iw), int(scale * ih) |
|
i = skimage.transform.resize(img, (dh, dw)) |
|
skimage.io.imsave(resized, i) |
|
|
|
print resized |
|
return resized |
|
|
|
|
|
def on_mouse_motion(canvas, mouse): |
|
global points |
|
if 0 < mouse.x < canvas.width and 0 < mouse.y < canvas.height: |
|
inner = randint(5, 35) |
|
outer = randint(inner, 45) |
|
pts = randint(5, 9) |
|
points.append((mouse.x, mouse.y, inner, outer, pts)) |
|
|
|
|
|
def setup(): |
|
global pic, pixels |
|
print 'usage: python starry.py "/path/to/filename"' |
|
print 'press control-s to save' |
|
filename = sys.argv[1] |
|
filename = resize_image(filename) |
|
print 'loading ...' |
|
pic = Pixels(Image(filename)) |
|
|
|
# Use a numpy array of pixels because pic.get is too slow to call repeatedly. |
|
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) |
|
|
|
print 'done loading' |
|
canvas.fps = 30 |
|
canvas.size = pic.size |
|
nostroke() |
|
canvas.on_mouse_motion = on_mouse_motion |
|
canvas.run(draw) |
|
|
|
|
|
def draw(canvas): |
|
global pic, pixels, points |
|
w, h = pixels.shape |
|
for (x, y, i, o, p) in points: |
|
star(x, y, points=p, outer=o, inner=i, fill=pixels[x][y]) |
|
|
|
image(pic, alpha=.2) |
|
|
|
|
|
if __name__ == '__main__': |
|
setup() |