python - 为什么我只能 "Import Image"?

标签 python python-imaging-library

在 python 中,我可以输入 import Image 来导入 Python 图像库 (PIL)。我可以直接输入它,而不是像其他人那样 from PIL import Image 。我怎么能做到这一点? from PIL import Imageimport Image 有什么区别吗?

我应该假设其他人不能做到这一点吗? (即编写代码分发给其他人时。)

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

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

最佳答案

看起来当您直接“导入 Image”时,该 Image 模块内部真正发生的事情是:“from PIL.Image import *”,它将所有名称从 PIL.Image 模块复制到 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 *

关于python - 为什么我只能 "Import Image"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25195011/

相关文章:

python - Tensorflow 对象检测 API 被杀死 - OOM。如何减少随机播放缓冲区的大小?

Python:如何从图像中切出具有特定颜色的区域(OpenCV、Numpy)

python - 用户数据的基本 for 循环问题

python - 确保在 Python 中捕获所有异常

javascript - 将数据从 Python 返回到 Javascript

python - PIL.ImageChops.subtract 和首先转换为 numpy 数组然后减去它们之间有什么区别?

python - 在 pygame 中显示的缓冲区在 tkinter 中不显示

python - 属性错误 : type object 'Image' has no attribute 'open'

python - 如何使用像素值/边框值将图像填充到特定尺寸(如 720*576)?

python - numpy.repeat 的反义词是什么?