Search code examples
pythonlistpixel

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


I am trying to extract pixel values from an image as a list:

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

But I am getting this error. Can anyone please help me?


Solution

  • It looks like you've redefined list. For example:

    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
    

    Don't use list as a variable name. You're code as show works as is, so there is some code missing that is assigning to list and causing the problem:

    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())
    >>>