Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created April 7, 2011 12:21
Show Gist options
  • Save aparrish/907653 to your computer and use it in GitHub Desktop.
Save aparrish/907653 to your computer and use it in GitHub Desktop.
drawing the average N-letter word using processing.py
import array
wordlen = 8
charwidth = 32
charheight = 64
def fillbuf(pg, font, word, buffer):
pg.beginDraw()
pg.background(0)
pg.fill(255)
pg.textAlign(LEFT, TOP)
pg.textFont(font)
pg.textSize(charwidth)
pg.text(word, 0, 0)
pg.endDraw()
for y in range(charheight):
for x in range(charwidth*wordlen):
buffer[x + y*(charwidth*wordlen)] += red(pg.get(x, y))
class Sketch(object):
def setup(self):
size(640, 480)
def draw(self):
font = createFont("Menlo", charwidth)
words = [word.strip() for word in open('wordlist.txt') if len(word.strip()) == wordlen]
buffer = array.array('d', [0.0] * (wordlen*charwidth * charheight))
pg = createGraphics(wordlen*charwidth, charheight, JAVA2D)
for i, word in enumerate(words):
if i % 50 == 0:
print word
fillbuf(pg, font, word, buffer)
background(0)
noStroke()
for y in range(charheight):
for x in range(charwidth*wordlen):
val = buffer[x + y*(charwidth*wordlen)]
fillval = (val / len(words))
fill(fillval)
rect(x, y, 1, 1)
noLoop()
save('output-%d.png' % wordlen)
sketch = Sketch()
def setup():
sketch.setup()
def draw():
sketch.draw()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment