【问题标题】:How to import multiple functions from PIL in python on Mac如何在 Mac 上的 python 中从 PIL 导入多个函数
【发布时间】:2017-03-16 08:52:24
【问题描述】:

我是 python 图像编辑的新手,仍在尝试了解有关 PIL 的内容。

我正在尝试在 python 中使用 PIL 在图像上创建自己的过滤器,但 python 不允许我导入多个函数。

from PIL import Image,Imagecolor,Imagefilter...

我想在计算机中使用图像,只是熟悉在 Python 中使用模块。

当我尝试使用一个功能时,如果我只导入 ImageFilter,它会说其他人无法识别..like..

import ImageFilter

img = Image.open('imagespuppy.jpg') 
out = img.filter(ImageFilter.DETAIL)

img.show()

它显示一个错误,说 undefined 'Image'

需要帮助!

【问题讨论】:

  • 在您的 from PIL 声明中,您没有正确地大写导入。 Imagecolor 必须是 ImageColorImageFilter 必须是 ImageFilter。这能解决您的问题吗?
  • 如果您想使用 Image 类,您应该将它与 ImageFilter 以及您可能使用的库中的任何其他内容一起导入。

标签: python macos python-imaging-library


【解决方案1】:

在 python 中,importfrom 用于包含其他模块。假设我有一个模块math,它有几个功能

  1. sqrt(平方根)
  2. mod(模数)

如果我想在我的另一个文件中包含数学模块并在没有模块名称的情况下使用它们 (math) 我必须这样做

from math import sqrt, mod
sqrt(25)

现在如果我必须使用他们的模块名称访问函数

import math
math.sqrt(25)

来到用户案例 Image 和 ImageFilter 是 PIL 模块的一部分。下面是你如何导入它

from PIL import Image, ImageFilter

img = Image.open('imagespuppy.jpg') 
out = img.filter(ImageFilter.DETAIL)

img.show()

【讨论】:

  • 成功了!!而且我还有另一个疑问,我看到了一些在终端上键入语句但没有在任何文本编辑器上键入语句的示例。这是如何运作的。它说这适用于交互式外壳,这是什么意思
  • 为什么不先从python开始,然后你可以切换到新的模块。您似乎在问基本问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2020-07-25
  • 2021-02-12
  • 2015-11-21
  • 2018-02-11
相关资源
最近更新 更多