#!/usr/bin/python import Image,ImageDraw,ImageFont size = (500,500) mode = 'RGBA' font = ImageFont.truetype( "Georgia.ttf", 20 ) def textbox( text="textbox", textwidth=400, margin=5 ): 'creates a textbox of a fixed width and variable height' words = text.split() words.reverse() s = [''] h = [margin] while words: w = words.pop() (wd,ht) = font.getsize(s[-1] + ' ' + w) if (wd > textwidth): s.append( w ) h.append( ht ) else: s[-1] += ' '+w (wd,ht) = font.getsize(s[-1] + ' ' + w) h.append( ht ) h.reverse() s.reverse() im = Image.new( mode, (textwidth+2*margin,sum(h)+margin) ) im.paste( (0,0,100), (0,0,textwidth+2*margin,sum(h)+margin) ) dr = ImageDraw.Draw( im ) x,y = margin,0 while s: ln = s.pop() y += h.pop() dr.text( (x,y), ln, font=font, fill=(255,255,0) ) return im def f(x,y): 'return a tuple (R,G,B)' return ((x and y) % 256, (x or y) % 256, (x+y) % 256) im = Image.new(mode,size) dr = ImageDraw.Draw(im) for x in range(size[0]): for y in range(size[1]): dr.point((x,y),fill=f(x,y)) # dr.text( (0,0), 'Aly', font=font ) w,h = im.size tb = textbox( 'alpha beta gamma delta eta something other whatever blah blah blah blah blah blah' ) ww,hh = tb.size mask = Image.new( 'L', tb.size ) mask.paste( 225, (0,0,ww,hh) ) mask.paste( 192, (5,5,ww-5,hh-5) ) im.paste( tb, (w-ww-5,h-hh-5), mask ) im.save('im.png')