ev3dev / ev3dev-lang-python

Pure python bindings for ev3dev
MIT License
422 stars 146 forks source link

Unicode characters doesn't work #783

Closed f1refa11 closed 1 year ago

f1refa11 commented 1 year ago

I wanted to render text in Russian, but when I tried to run the program, it gave me this error:

  File "/usr/lib/python3/dist-packages/PIL/ImageDraw.py", line 233, in text
    mask, offset = font.getmask2(text, self.fontmode)
AttributeError: 'ImageFont' object has no attribute 'getmask2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/robot/ev3tg/main.py", line 50, in <module>
    display.draw.text((4,4+18*idtext), "\u041f\u0440\u0438\u0432\u0435\u0442", font=fonts.load("charB12"))
  File "/usr/lib/python3/dist-packages/PIL/ImageDraw.py", line 237, in text
    mask = font.getmask(text, self.fontmode)
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-5: ordinal not in range(256)

How can i change the encoding?

f1refa11 commented 1 year ago

Solved the problem using custom ttf font and PIL.ImageFont.truetype("calibri.ttf", 12)