为什么Pillow不能识别JPEG格式?

时间:2014-01-15 02:51:03

标签: python python-imaging-library anaconda pillow

尝试使用这个简单的代码在图像上写文字:

import ImageFont
import Image
import ImageDraw

font = ImageFont.truetype("arial.ttf", 16)
img=Image.new("RGB", (200,200),(120,20,20))
draw = ImageDraw.Draw(img)
draw.text((0, 0),"This is a test",(255,255,0),font=font)
draw = ImageDraw.Draw(img)

img.save("C:/Users/User/Desktop/test","jpeg")

我收到此错误:

File "C:\Users\User\Anaconda\lib\site-packages\PIL\Image.py", line 1456, in save  
   save_handler = SAVE[format.upper()] # unknown format
KeyError: 'JPEG'

知道怎么解决这个问题吗?我在Windows 7中使用带有Eclipse Kepler和PyDev插件的Python 2.7.5 Anaconda版本。我还尝试img.save("test.jpeg")img.save("test.png")导致同样的错误。

2 个答案:

答案 0 :(得分:6)

原来Eclipse正在使用PIL而不是Pillow:我刚刚删除了Eclipse中的PIL库引用并确保使用Pillow而且运行正常。

答案 1 :(得分:1)

尝试运行

img.save("test.jpg")

看看是否有诀窍。如果您还提供有效的扩展名,则不必指定文件类型。