uploadcare / pillow-simd

The friendly PIL fork
https://python-pillow.github.io/pillow-perf/
Other
2.16k stars 85 forks source link

ImageFont needs an Update #95

Closed cedricholz closed 3 years ago

cedricholz commented 3 years ago

What did you do?

Tried to draw on an image with ImageDraw.

What did you expect to happen?

I expected text to be drawn on the image.

What actually happened?

The code that used to work no longer works when given an ImageFont.

What are your OS, Python and Pillow versions?

Code to reproduce:

base = Image.open("test_files/test_image.png")
txt = Image.new("RGBA", base.size, (255, 255, 255, 0))
font = ImageFont.truetype("fonts/FreeMono.ttf", 40)
d = ImageDraw.Draw(txt)
d.text(
    (10, 10),
    "Hello",
    font=font,
    fill=(255, 255, 255, 128)
)
txt.show()

I found the issue to be in ImageFont getmask2().

def getmask2(
  self,
  text,
  mode="",
  fill=Image.core.fill,
  direction=None,
  features=None,
  language=None,
  stroke_width=0,
  *args,
  **kwargs
):
    size, offset = self.font.getsize(text, direction, features, language)
    size = size[0] + stroke_width * 2, size[1] + stroke_width * 2
    im = fill("L", size, 0)
    self.font.render(
        text, im.id, mode == "1", direction, features, language, stroke_width
    )
    return im, offset

Specifically the mode == "1" no longer works. The error is TypeError: render() argument 3 must be str or None, not bool

The function is different on Pillow's repository. https://github.com/python-pillow/Pillow/blob/master/src/PIL/ImageFont.py

def getmask2(
    self,
    text,
    mode="",
    fill=Image.core.fill,
    direction=None,
    features=None,
    language=None,
    stroke_width=0,
    anchor=None,
    ink=0,
    *args,
    **kwargs,
):
    size, offset = self.font.getsize(
        text, mode, direction, features, language, anchor
    )
    size = size[0] + stroke_width * 2, size[1] + stroke_width * 2
    offset = offset[0] - stroke_width, offset[1] - stroke_width
    im = fill("RGBA" if mode == "RGBA" else "L", size, 0)
    self.font.render(
        text, im.id, mode, direction, features, language, stroke_width, ink
    )
    return im, offset

When used Pillow-SIMD with the new function it worked fine.

cedricholz commented 3 years ago

Was able to get working by reinstalling pipenv and adding correct flags.

pipenv shell pipenv --rm

export LDFLAGS="-L/usr/local/opt/zlib/lib" export CPPFLAGS="-I/usr/local/opt/zlib/include" export PKG_CONFIG_PATH="/usr/local/opt/zlib/lib/pkgconfig"

pipenv install --python 3.7