Tuesday, December 30, 2008

pyJoy






We just watched Control the other night and both my girlfriend and I liked a poster in the movie. Not knowing that this was an album cover, I immediately thought I should make something like that. So I wrote a script in a couple minutes to generate some images. Source code:





import cairo, sys
import math, random

def gen_row(w, smth=10, shft=0.2, wid=0.5):
mid = w/2
shft = mid*shft
wid = mid*wid

vals = [0]*w
for x in xrange(0, w):
sc = (x-random.uniform(mid-wid, mid+wid))/random.uniform(wid*0.9, wid*1.1)
sc = math.exp(-sc*sc/2)
ht = random.uniform(0,sc)
vals[x] = ht

for i in xrange(0, smth):
v = [0]*w
for x in xrange(1, w-1):
v[x] = (vals[x] + vals[x-1] + vals[x+1])/3
vals = v

return vals

w, h = 256, 512
x0, sc = 4, 20
wid = 0.2

if len(sys.argv)>=3:
w = int(sys.argv[1])
h = int(sys.argv[2])

if len(sys.argv)>=4:
wid = float(sys.argv[3])

surf = cairo.SVGSurface('joy.svg', w, h)
ctx = cairo.Context(surf)

for y in xrange(10, h, 5):

ctx.move_to(x0, y)
row = gen_row(w, wid=wid)

for x in xrange(x0, w-x0):
ctx.line_to(x, y-row[x]*sc)

ctx.set_source_rgba(0,0,0,1)
ctx.stroke_preserve()

ctx.set_source_rgba(1,1,1,1)
ctx.line_to(w-x0, y+10)
ctx.line_to(0, y+10)
ctx.line_to(0, y)
ctx.fill()

surf.finish()

No comments: