在尝试从图像中获取像素列表时出现错误 'list' object is not callable ,

while trying to make list of pixels from an image getting error 'list' object is not callable ,

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

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

但是我收到了这个错误。谁能帮帮我?

看来你重新定义了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.
>>> 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())
>>>

我试过了,对我有用。

from PIL import Image
i = Image.open("Images/image2.jpg")

pixels = i.load() # this is not a list, nor is it list()'able
width, height = i.size

all_pixels = []
for x in range(width):
    for y in range(height):
        #cpixel = pixels[x, y]
        #all_pixels.append(cpixel)
        cpixel = pixels[x, y]
        bw_value = int(round(sum(cpixel) / float(len(cpixel))))
            # the above could probably be bw_value = sum(cpixel)/len(cpixel)
        all_pixels.append(bw_value)