random textimage

G

gert

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@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@gert:~/Desktop/svn/xhtml$

i am stuck anybody can help me ?
 
J

James Stroud

gert said:
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@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@gert:~/Desktop/svn/xhtml$

i am stuck anybody can help me ?

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

James
 
G

gert

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

James

no this seems to work lol :)

import ImageFont, ImageDraw, Image

def gen(text):
image_file = "test.jpg"
image = Image.open(image_file)
font = "font.ttf"
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font, 12)
draw.text((1, 1), text,font=font)
image.save("temp.jpg")

if __name__ == "__main__":
import random
print gen(str(random.randint(0,1000)))

The next problem would be that i need this to work <img
src=randomtext.py/> in cherrypy because if two persons login at the
same time the first user get to see the second user textimage DOH!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top