已安装Python PIL/Pillow包但无法导入

8

我想进行一些图像处理,但遇到了一个问题。导入pillow模块似乎不起作用。我在这里找到了一个简单的脚本来检查安装了哪些包,我找到了它,但是导入它似乎不起作用。

这是我要运行的代码:

import pip

installed_packages = pip.get_installed_distributions()
installed_pillow_packages = [
    "%s==%s" % (i.key, i.version)
    for i in installed_packages
    if "pil" in i.key.lower()
]
print(installed_pillow_packages)

import pillow

以下是结果:

runfile('C:/Users/Augustas/.spyder2/temp.py', wdir=r'C:/Users/Augustas/.spyder2')
['pillow==2.6.1']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 601, in runfile
    execfile(filename, namespace)
  File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 66, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/Augustas/.spyder2/temp.py", line 7, in <module>
    import pillow
ImportError: No module named pillow

我正在使用Spyder和Python 2.7.9在Windows 8.1上运行此程序。

3个回答

26

您的导入方式不正确。请尝试使用以下方法:

import PIL
或者
from PIL import Image

PIL,即Python Imaging Library已经不再维护,现在使用Pillow代替。为了保持向后兼容性,在导入时仍需使用PIL模块名称。


5
实际上,Pillow被安装在名称为“PIL”的文件夹下,因此:
import PIL as pillow
from PIL import Image
...

看这里:

在此输入图像描述


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接