2015-04-03 154 views
1

我是相當新的Python和具有的PIL的保存功能出現問題時,我想運行下面的代碼保存PNG圖像與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 
+0

這似乎是你的字體問題,可能是PIL安裝,而不是保存。嘗試使用回溯的最後一行進行搜索:即「PIL IOError:解碼器壓縮不可用」,您會發現一些可能適用於您的情況的建議。 (很好的自我包含的問題,簡單的例子,順便說一句...一個很好的方法來獲得很好的答案!) – tom10 2015-04-04 17:42:35

回答

0

也許這是因爲您沒有在您的文本中應用任何字體。 試試這個:

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