'Unknown extension' 由于 EXTENSION 数组为空,在 PIL 的保存函数中
'Unknown extension' in save function of PIL due to empty EXTENSION array
我是 python 的新手,对 PIL 的 Pillow fork 的 save
功能有疑问。
用这个最小的例子
import Image
im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")
我收到以下错误:
File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 1667, in save
raise KeyError(ext) # unknown extension
KeyError: '.png'
save
函数中对应的行是
preinit()
[...]
try:
format = EXTENSION[ext]
except KeyError:
raise KeyError(ext) # unknown extension
我查看了 EXTENSION
数组并检测到它是空的,尽管它应该在 preinit()
中由例如 from PIL import PngImagePlugin
初始化。 PngImagePlugin.py
调用 Image.register_extension("PNG", ".png")
。观察此函数内或 PngImagePlugin
内的数组,它确实充满了文件扩展名。
将 print(EXTENSION)
放在 try-except-block 之前会显示一个空的 EXTENSION
数组。
(与 save
函数中几行下方的 SAVE
数组存在相同问题。)
感谢任何帮助。
编辑:我最近从 OpenSuse 13.1 升级。到 13.2。它在 13.1 中运行良好,但在 13.2 中运行不正常。
您需要这样写:
from PIL import Image # Notice the 'from PIL' at the start of the line
im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")
我是 python 的新手,对 PIL 的 Pillow fork 的 save
功能有疑问。
用这个最小的例子
import Image
im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")
我收到以下错误:
File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 1667, in save
raise KeyError(ext) # unknown extension
KeyError: '.png'
save
函数中对应的行是
preinit()
[...]
try:
format = EXTENSION[ext]
except KeyError:
raise KeyError(ext) # unknown extension
我查看了 EXTENSION
数组并检测到它是空的,尽管它应该在 preinit()
中由例如 from PIL import PngImagePlugin
初始化。 PngImagePlugin.py
调用 Image.register_extension("PNG", ".png")
。观察此函数内或 PngImagePlugin
内的数组,它确实充满了文件扩展名。
将 print(EXTENSION)
放在 try-except-block 之前会显示一个空的 EXTENSION
数组。
(与 save
函数中几行下方的 SAVE
数组存在相同问题。)
感谢任何帮助。
编辑:我最近从 OpenSuse 13.1 升级。到 13.2。它在 13.1 中运行良好,但在 13.2 中运行不正常。
您需要这样写:
from PIL import Image # Notice the 'from PIL' at the start of the line
im = Image.new("RGB", (200, 30), "#ddd")
im.save("image.png")