random textimage

James Stroud jstroud at mbi.ucla.edu
Wed Feb 28 06:13:47 EST 2007


gert wrote:
> import string
> import random
> import PIL
> from PIL import Image, ImageFont, ImageDraw
> from PIL import ImageEnhance, ImageOps, ImageStat
> from StringIO import StringIO
> import os
> 
> pwd = os.path.dirname(os.path.abspath(__file__))
> fpath=os.path.join(pwd,'img.ttf')
> iname=os.path.join(pwd,'pass.jpg')
> 
> def gen():
>     text = str(random.randint(0,1000))
>     im = Image.new("RGB", (125, 34), "#fff")
>     ttf = ImageFont.truetype(fpath, 16)
>     draw = ImageDraw.Draw(im)
>     draw.text((10,10), text, font=ttf, fill="green")
>     img = StringIO()
>     im.save(img, "JPEG")
>     f = open(iname)
>     f.write(im)
>     f.close()
>     return text
> 
> if __name__ == "__main__":
>     print gen()
> 
> gert at gert:~/Desktop/svn/xhtml$ python2.5 textimg.py
> Traceback (most recent call last):
>   File "textimg.py", line 27, in <module>
>     print gen()
>   File "textimg.py", line 22, in gen
>     f.write(im)
> TypeError: argument 1 must be string or read-only character buffer,
> not instance
> gert at gert:~/Desktop/svn/xhtml$
> 
> i am stuck anybody can help me ?
> 

Are you sure you don't want f.write(img) ?

James



More information about the Python-list mailing list