473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

random textimage

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 ?

Feb 28 '07 #1
2 1185
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@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
Feb 28 '07 #2
On Feb 28, 12:13 pm, James Stroud <jstr...@mbi.ucla.eduwrote:
gert wrote:
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
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!

Feb 28 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
2
by: madhuri.eerupula | last post by:
HI I had a bitmap image(textimage) on a picturebox, i want to do double click operation on it, if i double click on some part(particular text) of the pic box then i want to show that particular part...
6
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.