【问题标题】:Saving a png imaging with PIL使用 PIL 保存 png 图像
【发布时间】:2017-08-14 22:26:29
【问题描述】:

我对python比较陌生,当我想运行以下代码时,PIL的保存功能有问题

#!/usr/bin/env python
import ImageFont
import Image
import ImageDraw
from PIL import Image
img=Image.new("RGB", (200,200),(120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0))
draw = ImageDraw.Draw(img)
img.save("test.png")

我有这个错误我不知道为什么

Traceback (most recent call last):
  File "./ima2.py", line 9, in <module>
    draw.text((0, 0),"This is a test",(255,255,0))
  File "/usr/lib64/python2.7/site-packages/PIL/ImageDraw.py", line 260, in text
    font = self.getfont()
  File "/usr/lib64/python2.7/site-packages/PIL/ImageDraw.py", line 133, in getfont
    self.font = ImageFont.load_default()
  File "/usr/lib64/python2.7/site-packages/PIL/ImageFont.py", line 377, in load_default
    '''))))
  File "/usr/lib64/python2.7/site-packages/PIL/ImageFont.py", line 117, in _load_pilfont_data
    image.load()
  File "/usr/lib64/python2.7/site-packages/PIL/ImageFile.py", line 192, in load
    d = Image._getdecoder(self.mode, d, a, self.decoderconfig)
  File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 381, in _getdecoder
    raise IOError("decoder %s not available" % decoder_name)
IOError: decoder zip not available

【问题讨论】:

  • 这似乎是您的字体问题,可能是 PIL 安装问题,而不是保存问题。尝试使用回溯的最后一行进行搜索:即“PIL IOError:decoder zip not available”,您会发现一堆可能适用于您的情况的建议。 (措辞很好的问题,带有一个很好的独立的、最小的例子,顺便说一句......获得好答案的好方法!)

标签: image save python-imaging-library


【解决方案1】:

可能是因为您没有在文本中应用任何字体。 试试这个:

font = ImageFont.truetype("C:/Windows/Fonts/Arial.ttf", 24)             
draw.text((0, 0),"Sample Text",(255,255,255),font=font)

【讨论】:

    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 2013-01-05
    • 2018-11-21
    • 2016-04-11
    • 2013-03-21
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多