简体   繁体   English

初始化对象错误TypeError:“ int”对象没有属性“ __getitem__”

[英]Init object error TypeError: 'int' object has no attribute '__getitem__'

I am trying to do some image processing with the following code: 我正在尝试使用以下代码进行一些图像处理:

Image.open('C:\\temp\\img')
width, height = im.size
im  = im.resize((width*8,height*8), Image.BICUBIC)

pixdata = im.load()
for y in xrange(im.size[1]):
    for x in xrange(im.size[0]):
        if pixdata[x, y][0] < 165:
            pixdata = (0, 0, 0, 255)

for y in xrange(im.size[1]):
    for x in xrange(im.size[0]):
        if pixdata[x, y][1] < 165:
            pixdata = (0, 0, 0, 255)

for y in xrange(im.size[1]):
    for x in xrange(im.size[0]):
        if pixdata[x, y][2] > 0:
            pixdata[x, y] = (255, 255, 255, 255)

however when I am doing the above i get a TypeError. 但是,当我做上述时,我得到一个TypeError。 The pixdata is stored on memory properly but no idea why this is giving this error. 像素数据已正确存储在内存中,但不知道为什么会出现此错误。

if pixdata[x, y][0] < 165:
TypeError: 'int' object has no attribute '__getitem__'

Your problem is that you're reassigning pixdata . 您的问题是您要重新分配pixdata It starts out as an Image object, but then: 它以Image对象开始,但随后:

for y in xrange(im.size[1]):
    for x in xrange(im.size[0]):
        if pixdata[x, y][0] < 165:
            pixdata = (0, 0, 0, 255)

Now pixdata is (or could be; the if test may or may not pass) (0, 0, 0, 255) . 现在pixdata为(或可能为; if测试可以通过或可能不通过) (0, 0, 0, 255) pixdata (0, 0, 0, 255) Now the next time you try to access pixdata[x, y][0] (in a later iteration of that same loop, or in another loop later) you are trying to index into (0, 0, 0, 255) rather than the image data, and this obviously isn't going to work. 现在,下次您尝试访问pixdata[x, y][0] (在同一循环的后续迭代中,或在以后的另一循环中)时,您尝试索引为(0, 0, 0, 255)而不是图片数据,这显然行不通。

Solution: don't throw away pixdata if you still need it. 解决方案:如果仍然需要,请不要丢弃pixdata You want pixdata[x, y] = (0, 0, 0, 255) probably. 您可能希望pixdata[x, y] = (0, 0, 0, 255) You did it right in your third loop, make it the same way in your first two. 您在第三个循环中做了正确的事情,在前两个循环中做了同样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:“ int”对象没有属性“ __getitem__” - TypeError: 'int' object has no attribute '__getitem__' TypeError&#39;int&#39;对象没有属性&#39;__getitem__&#39; - TypeError 'int' object has no attribute '__getitem__' TypeError:&#39;int对象没有属性&#39;__getitem__&#39;……但是在哪里? - TypeError: 'int object has no attribute '__getitem__' … but where? AI Python-TypeError:“ int”对象没有属性“ __getitem__” - AI Python - TypeError: 'int' object has no attribute '__getitem__' 如何修复typeError&#39;int&#39;对象没有属性&#39;__getitem__&#39;? - How to fix typeError 'int' object has no attribute '__getitem__'? 如何解决TypeError:“ int”对象没有属性“ __getitem__”? - How to solve TypeError: 'int' object has no attribute '__getitem__'? TypeError:&#39;int&#39;对象在Python枚举中没有属性&#39;__getitem__&#39; - TypeError: 'int' object has no attribute '__getitem__' in Python enumerate TypeError:“ int”对象没有属性“ __getitem __”(值为0) - TypeError: 'int' object has no attribute '__getitem__' (the value is 0) TypeError:“ int”对象没有属性“ __getitem __”(简单问题) - TypeError: 'int' object has no attribute '__getitem__' (simple issue) scrapy exceptions.TypeError:&#39;int&#39;对象没有属性&#39;__getitem__&#39; - scrapy exceptions.TypeError: 'int' object has no attribute '__getitem__'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM