Skip to content

ImageDraw renders TrueType characters too skinny on 16-bit grayscale images compared to RGB ones #5591

@iprotsyuk

Description

@iprotsyuk

What did you do?

I tried to render a line of text on a 16-bit grayscale image using a TrueType font and the ImageDraw module.

What did you expect to happen?

I expected the shape of characters to be determined only by the actual font file and size, both of which I specified when initializing an instance of ImageFont.FreeTypeFont.

What actually happened?

For some reason the characters appear very skinny compared to the same line of text being rendered with the same font over an RGB image of the same size.

What are your OS, Python and Pillow versions?

  • OS: Linux
  • Python: 3.6
  • Pillow: 8.1.1
import numpy as np

from PIL import Image, ImageDraw, ImageFont

image_height = 500
image_width = 700

grayscale_image_array = np.ones([image_height, image_width]).astype(np.uint16)
grayscale_image_array[0:500, 0:700] = 12000
grayscale_image = Image.fromarray(grayscale_image_array)

rgb_image_array = np.ones([image_height, image_width]).astype(np.uint8)
rgb_image_array[0:500, 0:700] = 47  # roughly same color as on `grayscale_image`
rgb_image = Image.fromarray(rgb_image_array)

font_data = ImageFont.truetype('assets/ProductSans-Regular-prehint.ttf', 50)

grayscale_draw = ImageDraw.Draw(grayscale_image)
grayscale_draw.text((300, 300), "Expected normal-width characters", fill='white', font=font_data)

rgb_draw = ImageDraw.Draw(rgb_image)
rgb_draw.text((300, 300), "Expected normal-width characters", fill='white', font=font_data)

Grayscale image:
grayscale text rendering

RGB image:
rgb text rendering

ProductSans-Regular-prehint.ttf
(to test locally, change ".gif" extension of this file to ".ttf" as GitHub doesn't allow to upload those directly)

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