【问题标题】:Issue with image module on Python3Python3 上的图像模块问题
【发布时间】:2021-05-20 21:12:35
【问题描述】:

这是我的第一个问题(我正在尝试学习 Python 作为编程的起点)

我正在上第二节 Python 基础课,但作业有问题。

我在“图像处理”一章中,我正在尝试运行以下命令: #我有一个运行 Big Sur 11.3.1 的 MAC

import image

p = image.Pixel(45, 76, 200)
print(p.getRed())
p.setRed(66)
print(p.getRed())
p.setBlue(p.getGreen())
print(p.getGreen(), p.getBlue())

我必须安装“image”和“PIL”(一开始我无法导入它们)

我运行以下(安装它们)

python3 -m pip install image
python3 -m pip install PIL

我尝试导入图像模块(之后)

我试过了:

import image

还有:

from PIL import Image

但是当我尝试以下操作时,我不断收到错误消息:

bash-3.2$ python3
Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec  7 2020, 12:10:52) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import image
>>> p = image.Pixel(45, 76, 200)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'image' has no attribute 'Pixel'
>>>

或者,

bash-3.2$ python3
Python 3.9.1 (v3.9.1:1e5d33e9b9, Dec  7 2020, 12:10:52) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> p = image.Pixel(45, 76, 200)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'image' is not defined
>>> 

我不确定我是否没有正确安装/上传模块

我一直在尝试在线查找此内容,但运气不佳。

任何帮助/协助将不胜感激。

【问题讨论】:

    标签: python-3.x import


    【解决方案1】:

    .Pixel 应该在图像对象上调用以提供像素值。您现在没有图像对象。

    您已导入模块,但实际图像本身在哪里?您需要创建新图像或从文件中读取图像。

    如果您from PIL import Image,您可以使用Image.new(mode, size, color) 制作新的空白图像。查看此资源以获取示例:https://gethowstuff.com/create-new-image-python-pil/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多