Skip to main content
 首页 » 编程设计

python之为什么我只能 "Import Image"

2024年10月25日9TianFang

在 python 中,我可以键入 import Image 来导入 Python 图像库 (PIL)。我可以直接键入它而不是 from PIL import Image,就像其他人似乎所做的那样。我怎么可以这样做? from PIL import Image 和 import Image 有区别吗?

我是否应该假设其他人不能这样做? (即编写代码分发给他人时。)

我使用 Python 2.7.4。下面是一个在 REPL 中完美运行的代码示例:

> import Image 
> x = Image.new("RGB",(32,32),"blue) 
> x.save("test.png","PNG") 

请您参考如下方法:

看起来当您直接“导入图像”时,图像模块内部真正发生的事情是:“from PIL.Image import *”,它将 PIL.Image 模块中的所有名称复制到图像模块中。

查看源码:

In [1]: import Image 
 
In [2]: Image?? 
Type:        module 
String form: <module 'Image' from '/usr/lib/python2.7/dist-packages/PILcompat/Image.pyc'> 
File:        /usr/lib/python2.7/dist-packages/PILcompat/Image.py 
Source:      from PIL.Image import *