user3837937
user3837937

Reputation: 1

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. 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__'

Upvotes: 0

Views: 189

Answers (1)

kindall
kindall

Reputation: 184345

Your problem is that you're reassigning pixdata. It starts out as an Image object, but then:

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

Solution: don't throw away pixdata if you still need it. You want pixdata[x, y] = (0, 0, 0, 255) probably. You did it right in your third loop, make it the same way in your first two.

Upvotes: 1

Related Questions