我正在尝试使用PIL在灰度级png上写一些文本并跟随此线程。这似乎很简单,但我不确定我做错了什么。
然而,当我尝试这样做时,它会在draw.text
函数上消失:
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')
这是错误日志:
"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)
有人能指出我的问题吗?
答案 0 :(得分:3)
问题是png是8位灰阶。为了能够在8位图像上绘制,我必须在draw.text调用上使用单一颜色。换句话说:
# 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)
就是这样:))