镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Incorrect behavior of text=str.upper() in ImageDraw.Draw.text #5993

Closed

Description

@chiboreache

i'm using Arch Linux - everything is bleeding edge.

working

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


font_path = '/usr/share/fonts/TTF/Code2000.ttf'

m = 4
resolution = (256*m, 64*m)
fontsize = 28*m

image = Image.new('RGB', resolution, 'gray')

ImageDraw.Draw(image).text(
    (resolution[0]/2, resolution[1]/2),
    text='ქართული',
    font=ImageFont.truetype(font_path, fontsize),
    fill=(0, 0, 0),
    anchor="mm"
)

image.save('res.jpg')

2022-01-28x00:46:54

not working

ImageDraw.Draw(image).text(
    (resolution[0]/2, resolution[1]/2),
    text='ქართული'.upper(),
    font=ImageFont.truetype(font_path, fontsize),
    fill=(0, 0, 0),
    anchor="mm"
)

2022-01-28x00:47:06

Activity

  1. changed the title [-]incorrect behavior of `text=str.upper()` in ImageDraw.Draw.text[/-] [+]Incorrect behavior of `text=str.upper()` in ImageDraw.Draw.text[/+] on Jan 27, 2022
  2. radarhere commented on Jan 27, 2022

    @radarhere
    Member

    Hi. What font are you using?

  3. chiboreache commented on Jan 27, 2022

    @chiboreache
    Author

    I found similar behaviour in many fonts Code2000, DejaVuSans, DejaVuSerif, FreeSerif, FreeMonoBold ...

  4. radarhere commented on Jan 27, 2022

    @radarhere
    Member

    As with #5948, it is important to understand that fonts do not automatically support every character you might try.

    Starting with code from https://stackoverflow.com/questions/4458696/finding-out-what-characters-a-given-font-supports and making changes, here is code (using fonttools rather than Pillow) that checks if the characters you are trying to draw are supported by the font.

    from itertools import chain
    from fontTools.ttLib import TTFont
    ttf = TTFont("Code2000.ttf", 0, allowVID=0, ignoreDecompileErrors=True, fontNumber=-1)
    chars = []
    for x in ttf["cmap"].tables:
        chars += [y[0] for y in x.cmap.items()]
    ttf.close()
    
    print("original string")
    text = 'ქართული'
    for letter in text:
        if ord(letter) in chars:
            print(letter, ord(letter), "is in font")
        else:
            print(letter, ord(letter), "is missing from font", ord(letter))
    
    print()
    print("uppercase string")
    text = text.upper()
    for letter in text:
        if ord(letter) in chars:
            print(letter, ord(letter), "is in font")
        else:
            print(letter, ord(letter), "is missing from font")

    Running the code, I get

    original string
    ქ 4325 is in font
    ა 4304 is in font
    რ 4320 is in font
    თ 4311 is in font
    უ 4323 is in font
    ლ 4314 is in font
    ი 4312 is in font
    
    uppercase string
    Ქ 7333 is missing from font
    Ა 7312 is missing from font
    Რ 7328 is missing from font
    Თ 7319 is missing from font
    Უ 7331 is missing from font
    Ლ 7322 is missing from font
    Ი 7320 is missing from font
    

    So I conclude that the font is missing the 'upper' version of your characters.
    For example, https://www.compart.com/en/unicode/U+10E5 is supported by Code2000, but https://www.compart.com/en/unicode/U+1CA5 is not.

  5. chiboreache commented on Jan 27, 2022

    @chiboreache
    Author

    it would be nice to have built in char checker, but whatever, thank you @radarhere !

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions