Python 尝试从图像中创建像素列表时出现错误';列表';对象不可调用,
我试图从图像中提取像素值作为列表:Python 尝试从图像中创建像素列表时出现错误';列表';对象不可调用,,python,list,pixels,Python,List,Pixels,我试图从图像中提取像素值作为列表: 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
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
从PIL导入图像
im=Image.open('exp.jpg','r')
pix_val=list(im.getdata())
pix_val_flat=[x表示集合中的集合,pix_val表示集合中的x]
打印(pix_val_平面)
错误:
文件“C:/Users/anupa/Desktop/All Files/LZW/Code/image.py”,第4行,在
pix_val=list(im.getdata())
TypeError:“列表”对象不可调用
但我得到了这个错误。有人能帮我吗?看起来你已经重新定义了
列表
。例如:
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
我试过这个,它对我有效
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)
请参见此处的评论:。这可能是因为您正在将im.getdata()强制转换为list()Thnaks。。谢谢你为我工作。它说
list
(变量)不可调用。这意味着列表类型已被重新定义。您是否有要在代码前面列出的作业(例如,list=something
)print(type(list))
应该返回
。如果您不在某处重新定义list
,您的原始代码就可以工作。请参阅我的答案,其中给出了您报告的错误。
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)