Need help from someone that have PIL installed

A

alejandro

Can please someone run this little script that should output characters like
¾æè¹ð in an image.
If it does it correctly can you tell me what OS, python version & PIL
version you have?
Or better if someone can tell me why this is not working properly on my PC?
(Win XP, PIL 1.1.6., Python 2.6...)
I don't recive any error it's just that the characters outputed in the image
are not characters... more like symbols...

# encoding:utf-8

from PIL import Image
import ImageDraw
import ImageFont


img = Image.new("RGBA",(250,40))

#----------------------------------- making the image transparent
pixdata = img.load()

for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y] == (255, 255, 255, 255):
pixdata[x, y] = (255, 255, 255, 0)

#--------------------------------- drawing text
draw = ImageDraw.Draw(img)
arial = ImageFont.truetype("arial.ttf",32) # needs path to font, my font
was in the same folder as the script
string = "proba test ¾æèð¹"
draw.text((20,8),string ,font=arial, fill="red")

# write
img.save("img2.png", "PNG")
 
P

Peter Otten

alejandro said:
Can please someone run this little script that should output characters
like ����� in an image.
If it does it correctly can you tell me what OS, python version & PIL
version you have?
Or better if someone can tell me why this is not working properly on my
PC? (Win XP, PIL 1.1.6., Python 2.6...)
I don't recive any error it's just that the characters outputed in the
image are not characters... more like symbols...

# encoding:utf-8

from PIL import Image
import ImageDraw
import ImageFont

# the last tuple is the background color
img = Image.new("RGBA",(300, 50), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)
arial = ImageFont.truetype("arial.ttf",32) # needs path to font, my font
was in the same folder as the script

# I think that the PIL can cope with unicode, so add a u-prefix here:
text = u"proba test �����"
draw.text((20,8), text ,font=arial, fill="red")
# write
img.save("img2.png", "PNG")

Peter
 
A

alejandro

# the last tuple is the background color
img = Image.new("RGBA",(300, 50), (0, 0, 0, 0))
Thank you for this....
# I think that the PIL can cope with unicode, so add a u-prefix here:
text = u"proba test ¾æèð¹"
draw.text((20,8), text ,font=arial, fill="red")

Nope i gives:
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0x9e in position
0: unexpected code byte

and without the encoding :
SyntaxError: Non-ASCII character '\x9e' in file C:\Documents and
Settings\Perc\My Documents\NetBeansProjects\venus_header\src\venus_header.py
on line 16, but no encoding declared; see
http://www.python.org/peps/pep-0263.html for details
 
P

Peter Otten

alejandro said:
Thank you for this....


Nope i gives:
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0x9e in
position 0: unexpected code byte

and without the encoding :
SyntaxError: Non-ASCII character '\x9e' in file C:\Documents and
Settings\Perc\My
Documents\NetBeansProjects\venus_header\src\venus_header.py on line 16,
but no encoding declared; see http://www.python.org/peps/pep-0263.html for
details

Make sure that

# encoding:utf-8

is the first line of your script, details and fineprint here:

http://www.python.org/dev/peps/pep-0263/

Peter
 
P

Peter Otten

alejandro said:
Tryed that...

What happened?
What was the output of my script on your computer?

$ python -V
Python 2.6.4
$ python -c "import Image; print Image.VERSION"
1.1.6
$ cat draw_text.py
# encoding:utf-8

from PIL import Image
import ImageDraw
import ImageFont

img = Image.new("RGBA",(300, 50), (0, 0, 0, 0))
draw = ImageDraw.Draw(img)

FONT = "/usr/share/fonts/truetype/msttcorefonts/arial.ttf"

arial = ImageFont.truetype(FONT, 32)
text = u"proba test ¾æèð¹"
print repr(text)
draw.text((20,8), text, font=arial, fill="red")
img.save("tmp.png")
$ python draw_text.py
u'proba test \xbe\xe6\xe8\xf0\xb9'

The image looks as expected.

Peter
 
T

Thomas Jollans

Thank you for this....


Nope i gives:
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0x9e in position
0: unexpected code byte

Is the file, which you claim is UTF-8 encoded, actually UTF-8 encoded?
If you're not sure, explicitly tell your text editor to save the file as
UTF-8, and then try again.
 
A

alejandro

Is the file, which you claim is UTF-8 encoded, actually UTF-8 encoded?
If you're not sure, explicitly tell your text editor to save the file as
UTF-8, and then try again.
I feel like an idiot... haven't used Python for some time... my editor was
set for utf-8 on PHP projects and other... but somehow i forgot to set for
py files too... thanks both of you!
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top