#!/Library/Frameworks/Python.framework/Versions/Current/bin/python #voldemort.py from PIL import Image, ImageFont, ImageDraw filenames = ['book%d.txt' % x for x in range(1,8)] width,height = 1100,700 img = Image.new("RGB",(width,height)) font = ImageFont.truetype("Courier New.ttf",22) bigfont = ImageFont.truetype("Courier New.ttf",40) draw = ImageDraw.Draw(img) gap = 20 frame = ((150,200),(width-150-gap,height-200-gap) ) rowHeight = (frame[1][1])/len(filenames) draw.text((420, 130), 'Voldemort', font=bigfont, fill=(255,0,0)) draw.text((420, 70), 'He-who-must-not-be-named', font=bigfont, fill=(0,0,255)) draw.text((420, 10), 'You-Know-Who', font=bigfont, fill=(0,255,0)) draw.text((20, 70), 'Occurences of:', font=bigfont, fill=(255,255,255)) draw.text((frame[0][0]+5,frame[0][1]-25), 'Beginning of book', font=font, fill=(150,150,150)) draw.text((frame[0][0]+frame[1][0]-60,frame[0][1]-25), 'End', font=font, fill=(150,150,150)) def positionsOf(phrase, text): bookLength = len(text) results = [] result = text.find(phrase) while result is not -1 : results.append(float(result)/bookLength) result = text.find(phrase,result+1) return results for i in range(len(filenames)): #draw.rectangle(((0,i*rowHeight+gap/2.0),(width,rowHeight-gap)), outline=(66,66,66), fill=(0,0,0)) myRect = ((frame[0][0],i*rowHeight+gap/2.0+frame[0][1]),(frame[1][0],rowHeight-gap)) booktxt = open(filenames[i]).read() bookLength = len(booktxt) print filenames[i] + 'has ' + `bookLength` + ' characters ' booktxt = booktxt.lower() voldemorts = positionsOf('voldemort', booktxt) notbenamed = positionsOf('he-who-must-not-be-named', booktxt) youknowwho = positionsOf('you-know-who', booktxt) print 'found ' + `len(voldemorts)` + ' voldemorts' print 'found ' + `len(notbenamed)` + ' notbenamed' print 'found ' + `len(youknowwho)` + ' youknowwho' for v in voldemorts: draw.line( ( myRect[0][0]+v*myRect[1][0], myRect[0][1], myRect[0][0]+v*myRect[1][0], myRect[0][1]+myRect[1][1] ), fill=(255,0,0) ) for v in notbenamed: draw.line( ( myRect[0][0]+v*myRect[1][0], myRect[0][1], myRect[0][0]+v*myRect[1][0], myRect[0][1]+myRect[1][1] ), fill=(0,0,255) ) for v in youknowwho: draw.line( ( myRect[0][0]+v*myRect[1][0], myRect[0][1], myRect[0][0]+v*myRect[1][0], myRect[0][1]+myRect[1][1] ), fill=(0,255,0) ) draw.text((15, i * rowHeight + frame[0][1]+rowHeight/2.0), 'Book %d'%(i+1), font=font, fill=(255,255,255)) print 'Drawing text at ' + `(0, i*rowHeight+rowHeight/2.0)` """ splitted = booktxt.split() print 'splitted has ' + `len(splitted)` + ' elements here are a few ' + `splitted[:100]` for j in range(len(splitted)): if j.lowercase() is 'voldemort' """ img.save("voldemort.png") img.show()