1

I am trying to write some text over a grayscale png using PIL and following this thread. It seems pretty straightforward but I am not sure what I am doing wrong.

Add Text on Image using PIL

Yet, when I try to do it dies on the draw.text function:

from PIL import Image, ImageDraw, ImageFont
img = Image.open("test.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("open-sans/OpenSans-Regular.ttf", 8)
# crashes on the line below:
draw.text((0, 0), "Sample Text", (255, 255, 255), font=font)
img.save('test_out.png')

This is the error log:

"C:\Python27\lib\site-packages\PIL\ImageDraw.py", line 109, in _getink
ink = self.draw.draw_ink(ink, self.mode)
TypeError: function takes exactly 1 argument (3 given)

Can anyone point me out the issue?

Community
  • 1
  • 1
gmmo
  • 2,577
  • 3
  • 30
  • 56
  • It works for me with Python 2.7.13 and Pillow 4.1.0 on macOS, although I did `img = Image.new("RGB", (300, 300))` instead of opening an existing image. What's the full traceback? What version of Pillow are you using? – Hugo May 01 '17 at 16:33
  • @Hugo see my answer. I had to do some hacking :) – gmmo May 01 '17 at 18:18

1 Answers1

8

the issue is that the png was an 8-bit gray scale. To be able to draw on top of 8-bit images, I have to use a single color on the draw.text call. In other words:

# this works only for colored images
draw.text((0, 0), "Sample Text", (255, 255, 255), font=font)

# 8-bit gray scale , just pass one value for the color
# 0 = full black, 255 = full white
draw.text((0, 0), "Sample Text", (255), font=font)

this is it :)

gmmo
  • 2,577
  • 3
  • 30
  • 56