Hi
I am trying to create an image with text hebrew in it using Image and ImageDraw moduls but I keep getting erros. this is the code
import Image
import ImageDraw
a = u'\u05d0'#the unicode of the letter I want to display
im=Image.new('RGB',(200,200),(100,100,100))
d=ImageDraw.Draw(im)
d.text((0,0),a)

this is the error I get:

Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
d.text((0,0),a)
File "C:\Python26\lib\site-packages\PIL\ImageDraw.py", line 267, in text
mask = font.getmask(text, self.fontmode)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u05d0' in position 0: ordinal not in range(128)

any help would be appriciated

thanks

Recommended Answers

All 4 Replies

We can't help without knowing what encoding your program uses (obviously not the right one). It appears to work fine with the universal utf-8 encoding, see here http://effbot.org/pyfaq/what-does-unicodeerror-ascii-decoding-encoding-error-ordinal-not-in-range-128-mean.htm

a = unicode('\u05d0', "utf-8")
print a

hi
I tried to use unicode as you suggested but it simply wrote on the BMP the actual letters '\u05d0'.

I want to write to the screen hebrew lettering, for example ('אריאל')
how can I chack the encoding my machine is using?

or better yet what should I change iorder for the code to work?

import Image
import ImageDraw
a = "אריאל" #or any other hebrew string
im=Image.new('RGB',(200,200),(100,100,100))
d=ImageDraw.Draw(im)
d.text((0,0),a)
im.show()


thanks

Do you have Hebrew fonts installed on your system? Try the following and see if it prints an Alef. I don't know much about Hebrew, but it appears that the iso8859_8 encoding should be used. Note that all of this depends on which Python version you are using. I tested this on Python 2.6.4

a = unichr(1488)
b = a.encode("iso8859_8")
print "b =",b

Do you have Hebrew fonts installed on your system? Try the following and see if it prints an Alef. I don't know much about Hebrew, but it appears that the iso8859_8 encoding should be used. Note that all of this depends on which Python version you are using. I tested this on Python 2.6.4

a = unichr(1488)
b = a.encode("iso8859_8")
print "b =",b

Hi
when I print it from the shall python (print a) it works fine. the problem is that when I use d.text((0,0),a) in order to draw it to a bitmap , it write some other ascii lettering

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.