枕头 - 尝试在图像上写文字时出现 KeyError
Pillow - KeyError when trying to write text on an image
我正在尝试在 Pillow 中的图像上写一些文本,但出于某种原因,它似乎经常出现关于 KeyError 的错误。我已经用多个预先编写的示例对此进行了测试,所以我很确定这不是我的代码。这是脚本的副本和我遇到的错误:
from PIL import Image, ImageFont, ImageDraw
font = ImageFont.truetype("F25_Bank_Printer.ttf", 16)
img = Image.open('background.png')
title_text = "AUUUUUGH"
image_editable = ImageDraw.Draw(img)
image_editable.text((15,15), title_text, (237,230,211), font=font)
img.save('test_card.png')
Traceback (most recent call last):
File "/usr/lib64/python3.9/site-packages/PIL/ImagePalette.py", line 99, in getcolor
return self.colors[color]
KeyError: (237, 230, 211)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/hbag/git/novelty-scripts/scripts/probebadge.py", line 9, in <module>
image_editable.text((15,15), title_text, (237,230,211), font=font)
File "/usr/lib64/python3.9/site-packages/PIL/ImageDraw.py", line 339, in text
ink = getink(fill)
File "/usr/lib64/python3.9/site-packages/PIL/ImageDraw.py", line 302, in getink
ink, fill = self._getink(fill)
File "/usr/lib64/python3.9/site-packages/PIL/ImageDraw.py", line 112, in _getink
ink = self.palette.getcolor(ink)
File "/usr/lib64/python3.9/site-packages/PIL/ImagePalette.py", line 109, in getcolor
self.palette[index + 256] = color[1]
IndexError: bytearray index out of range
而且,为了安全起见,这是我尝试绘制的图像:
您的问题可能是您尝试编辑的图像已编入索引,这意味着它没有可用的 RGB 颜色,只有有限的调色板。所以 (237,230,211)
不是 RGB 元组,而是图像调色板的索引。
通过在尝试绘制之前将图像转换为 RGB,您应该能够使其工作:
from PIL import Image, ImageFont, ImageDraw
font = ImageFont.truetype("F25_Bank_Printer.ttf", 16)
img = Image.open('background.png')
img = img.convert('RGB')
title_text = "AUUUUUGH"
image_editable = ImageDraw.Draw(img)
image_editable.text((15,15), title_text, (237,230,211), font=font)
img.save('test_card.png')
我正在尝试在 Pillow 中的图像上写一些文本,但出于某种原因,它似乎经常出现关于 KeyError 的错误。我已经用多个预先编写的示例对此进行了测试,所以我很确定这不是我的代码。这是脚本的副本和我遇到的错误:
from PIL import Image, ImageFont, ImageDraw
font = ImageFont.truetype("F25_Bank_Printer.ttf", 16)
img = Image.open('background.png')
title_text = "AUUUUUGH"
image_editable = ImageDraw.Draw(img)
image_editable.text((15,15), title_text, (237,230,211), font=font)
img.save('test_card.png')
Traceback (most recent call last):
File "/usr/lib64/python3.9/site-packages/PIL/ImagePalette.py", line 99, in getcolor
return self.colors[color]
KeyError: (237, 230, 211)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/hbag/git/novelty-scripts/scripts/probebadge.py", line 9, in <module>
image_editable.text((15,15), title_text, (237,230,211), font=font)
File "/usr/lib64/python3.9/site-packages/PIL/ImageDraw.py", line 339, in text
ink = getink(fill)
File "/usr/lib64/python3.9/site-packages/PIL/ImageDraw.py", line 302, in getink
ink, fill = self._getink(fill)
File "/usr/lib64/python3.9/site-packages/PIL/ImageDraw.py", line 112, in _getink
ink = self.palette.getcolor(ink)
File "/usr/lib64/python3.9/site-packages/PIL/ImagePalette.py", line 109, in getcolor
self.palette[index + 256] = color[1]
IndexError: bytearray index out of range
而且,为了安全起见,这是我尝试绘制的图像:
您的问题可能是您尝试编辑的图像已编入索引,这意味着它没有可用的 RGB 颜色,只有有限的调色板。所以 (237,230,211)
不是 RGB 元组,而是图像调色板的索引。
通过在尝试绘制之前将图像转换为 RGB,您应该能够使其工作:
from PIL import Image, ImageFont, ImageDraw
font = ImageFont.truetype("F25_Bank_Printer.ttf", 16)
img = Image.open('background.png')
img = img.convert('RGB')
title_text = "AUUUUUGH"
image_editable = ImageDraw.Draw(img)
image_editable.text((15,15), title_text, (237,230,211), font=font)
img.save('test_card.png')