Skip to content

ImageDraw.Draw.textsize fails with Indic fonts #2620

Closed

Description

@ChillarAnand

What did you do?

import os

from PIL import Image, ImageFont, ImageDraw


mode = 'RGB'
size = (100, 64)

im = Image.new(mode, size)
draw = ImageDraw.Draw(im)

fonts_dir = os.path.expanduser('~/.fonts/')

font_telugu = ImageFont.truetype(os.path.join(fonts_dir, "Vani.ttf"), 28)
text = "నిత్య"
w, h = draw.textsize(text)

What did you expect to happen?

Get width/height

What actually happened?

>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/chillaranand/projects/likitham/create_likitham_dataset.py", line 16, in <module>
    w, h = draw.textsize(text)
  File "/home/chillaranand/.virtualenvs/py35/lib/python3.5/site-packages/Pillow-4.2.0-py3.5-linux-x86_64.egg/PIL/ImageDraw.py", line 260, in textsize
  File "/home/chillaranand/.virtualenvs/py35/lib/python3.5/site-packages/Pillow-4.2.0-py3.5-linux-x86_64.egg/PIL/ImageFont.py", line 110, in getsize
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 0-4: ordinal not in range(256)
>>> 

What versions of Pillow and Python are you using?

pip show pillow    
Name: Pillow
Version: 4.2.0
Summary: Python Imaging Library (Fork)
Home-page: https://python-pillow.org
Author: Alex Clark (Fork Author)
Author-email: [email protected]
License: Standard PIL License
Location: /home/chillaranand/.virtualenvs/py35/lib/python3.5/site-packages/Pillow-4.2.0-py3.5-linux-x86_64.egg
Requires: olefile

I have raqm installed.

Activity

  1. wiredfool commented on Jul 14, 2017

    @wiredfool
    Member

    I've replicated this on 3.5, but not 2.7.

  2. added
    Bug
    on Jul 14, 2017
  3. wiredfool commented on Jul 18, 2017

    @wiredfool
    Member

    Your bug is here:

    w, h = draw.textsize(text)
    

    You need to pass in the font, or it draws with the default bitmapped font that's 8 bit only.

    e.g.:

    w, h = draw.textsize(text, font=font_telugu)
    
  4. removed
    Bug
    on Jul 18, 2017
  5. ChillarAnand commented on Jul 18, 2017

    @ChillarAnand
    Author

    Thanks @wiredfool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions