Python 使用PIL在图像上添加文本。错误消息
我有以下代码可以下载图像并在图像上写入文本。下载效果很好。错误发生在draw.text行上。我不知道为什么我会出错。ttf文件位于正确的位置,路径名正确。我用pip安装了枕头。我没有遇到任何错误Python 使用PIL在图像上添加文本。错误消息,python,python-imaging-library,Python,Python Imaging Library,我有以下代码可以下载图像并在图像上写入文本。下载效果很好。错误发生在draw.text行上。我不知道为什么我会出错。ttf文件位于正确的位置,路径名正确。我用pip安装了枕头。我没有遇到任何错误 import urllib urlSA = "http://www.wpc.ncep.noaa.gov/archives/sfc/" + year + "/usfntsfc" + year + month + day + "09.gif" savedFileSA = "C:/images/sa2000
import urllib
urlSA = "http://www.wpc.ncep.noaa.gov/archives/sfc/" + year + "/usfntsfc" + year + month + day + "09.gif"
savedFileSA = "C:/images/sa2000/" + month + day + yearShort + ".jpg"
urllib.urlretrieve (urlSA, savedFileSA)
# Add a title to the SA image
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
img = Image.open(savedFileSA)
draw = ImageDraw.Draw(img)
fnt = ImageFont.truetype("C:/images/arial.ttf", 12)
draw.text((10, 10),"Sample Text",(3,3,3),font=fnt)
img.save(savedFileSA)
错误:
Traceback (most recent call last):
File "C:\images\storm_reports_Arc103.py", line 105, in <module>
draw.text((10, 10),"Sample Text",(3,3,3),font=fnt)
File "C:\Python27\ArcGIS10.3\lib\site-packages\PIL\ImageDraw.py", line 253, in text
ink, fill = self._getink(fill)
File "C:\Python27\ArcGIS10.3\lib\site-packages\PIL\ImageDraw.py", line 129, in _getink
ink = self.palette.getcolor(ink)
File "C:\Python27\ArcGIS10.3\lib\site-packages\PIL\ImagePalette.py", line 101, in getcolor
self.palette = [int(x) for x in self.palette]
ValueError: invalid literal for int() with base 10: ''
>>>
回溯(最近一次呼叫最后一次):
文件“C:\images\storm\u reports\u Arc103.py”,第105行,在
draw.text((10,10),“示例文本”(3,3,3),font=fnt)
文本文件“C:\Python27\ArcGIS10.3\lib\site packages\PIL\ImageDraw.py”,第253行
墨水,填充=自身。\u获取墨水(填充)
文件“C:\Python27\ArcGIS10.3\lib\site packages\PIL\ImageDraw.py”,第129行,在\u getink中
ink=self.palette.getcolor(ink)
文件“C:\Python27\ArcGIS10.3\lib\site packages\PIL\imagepalete.py”,第101行,在getcolor中
self.palete=[int(x)表示self.palete中的x]
ValueError:基数为10的int()的文本无效:“”
>>>
问题在于GIF图片被调色板化。如果先将其转换为RGB,代码将正常工作
换线
img = Image.open(savedFileSA)
到
下面是在我的机器上工作的代码。它下载一张图片并在上面放一个大的绿色“示例文本”。请注意,我在OSX上,因此我的路径可能与您的不同
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import urllib
urlSA = "http://www.wpc.ncep.noaa.gov/archives/sfc/2015/usfntsfc2015010518.gif"
savedFileSA = "andrew.gif"
urllib.urlretrieve (urlSA, savedFileSA)
img = Image.open(savedFileSA).convert("RGB")
draw = ImageDraw.Draw(img)
fnt = ImageFont.truetype("/Library/Fonts/Comic Sans MS.ttf", 72)
draw.text((10, 10), "Sample Text", (0, 128, 0), font=fnt)
img.show()
img.save("andrew.jpg")
您正在将
.gif
另存为.jpg
,我想这与此有很大关系。我将其改回另存为.gif,但仍然会出现相同的错误。是的,我想是的。gif图像在文件夹中。我删除了“(3,3,3)”代码,它成功了。也许我需要在我的机器上安装更多的颜色或其他东西?我删除了它,它对我也有效,尽管我从你那里得到了不同的错误。GIF最多只能有256种颜色。
import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import urllib
urlSA = "http://www.wpc.ncep.noaa.gov/archives/sfc/2015/usfntsfc2015010518.gif"
savedFileSA = "andrew.gif"
urllib.urlretrieve (urlSA, savedFileSA)
img = Image.open(savedFileSA).convert("RGB")
draw = ImageDraw.Draw(img)
fnt = ImageFont.truetype("/Library/Fonts/Comic Sans MS.ttf", 72)
draw.text((10, 10), "Sample Text", (0, 128, 0), font=fnt)
img.show()
img.save("andrew.jpg")