Python 2.7 Can';t使用PIL和python 2.7将文本写入灰度PNG

Python 2.7 Can';t使用PIL和python 2.7将文本写入灰度PNG,python-2.7,python-imaging-library,pillow,Python 2.7,Python Imaging Library,Pillow,我正在尝试使用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) # cra

我正在尝试使用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)

有人能告诉我这个问题吗?

问题是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)

就是这样:)

问题是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)

这就是它:)

它适用于我在macOS上使用Python2.7.13和Pillow 4.1.0,尽管我没有打开现有图像,而是使用了
img=Image.new(“RGB”,(300300))
。什么是完整的回溯?“你用的枕头是什么版本的?”雨果看到我的答案了。我不得不做一些修改:)它在macOS上的Python2.7.13和Pizzle 4.1.0上对我有效,尽管我没有打开现有的图像,而是做了
img=Image.new(“RGB”,(300300))
。什么是完整的回溯?“你用的枕头是什么版本的?”雨果看到我的答案了。我不得不做一些黑客操作:)