python - 在尝试从图像中创建像素列表时出现错误 'list' 对象不可调用,

标签 python list pixel

我正在尝试从图像中提取像素值作为列表:

from PIL import Image

im = Image.open('exp.jpg','r')
pix_val = list(im.getdata())
pix_val_flat = [x for sets in pix_val for x in sets]
print(pix_val_flat)

Error: 
  File "C:/Users/anupa/Desktop/All Files/LZW/Code/image.py", line 4, in <module>
    pix_val = list(im.getdata())

TypeError: 'list' object is not callable

但我收到此错误。有人可以帮我吗?

最佳答案

看来您已经重新定义了列表。例如:

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> list()  # list is certainly callable...
[]
>>> type(list)
<class 'type'>
>>> list = [1,2,3]  # Now list is used as a variable and reassigned.
>>> type(list)
<class 'list'>
>>> list()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'list' object is not callable

不要使用列表作为变量名。您的代码按原样显示,因此缺少一些分配给 list 并导致问题的代码:

Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import Image
>>> im = Image.open('exp.jpg','r')
>>> pix_val = list(im.getdata())
>>>

关于python - 在尝试从图像中创建像素列表时出现错误 'list' 对象不可调用,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49241059/

相关文章:

python - 使用 OpenCV 和 mask 合并图像

java - 节点数组如何工作?

c++ - 从 SDL_Texture 访问像素颜色

python - 如何找到用 matplotlib 绘制的图传递的像素

Python - 这是初始化空集矩阵的正确方法吗?

Python:将图像和数据帧写入同一个 excel 文件

python - 如何创建一个自下而上的合并排序函数来改变输入列表?

java - 在 Java 中处理 JPEG 图像

python - 如何查询数据库以获得一个列表,其中每个值是具有相同日期值的记录数之和?

python - 是否可以加快Python中列表到数组的转换速度?