Python - 'int' object has no attribute '__getitem__'

863 views Asked by At

I've searched the site, it is true there are other topics with the same name and the same problems, but I tried to read and apply the changes to my program with no results, so I decided to open a new one.

I need to create a class Pixels (which I created with no problem), and a class PImage, which instead I found the problem:
(carry only the most important methods)

class Pixel:

class Pixel(object):
    def __init__(self, r, g, b, op): // op = opacity
        self.r, self.g, self.b, self.op = r, g, b, op

    def __sub__(self, other):
        return self.r-other.r,self.g-other.g,self.b-other.b

class PImage

class PImage(object):
    def __init__(self, fname=None, size=None):
        if(fname!=None):
            img_app = image.load(fname)
            self.immagine = img_app
            w, h = len(img_app[0]), len(img_app)

            for j in range(h):
                for i in range(w):
                    self.immagine[j][i] = [Pixel(img_app[j][i][0], img_app[j][i][1], img_app[j][i][2], 255)]

        elif(size!=None):
            self.immagine = image.create(size[0], size[1], (0,0,0, 255))

    def size(self):
        return len(self.immagine[0]), len(self.immagine)

    def get_pixel(self, x, y):
        if image.inside(x, y, self.immagine):
            return self.immagine[y][x]
        else:
            return None

    def opacity(self, x, y, t=150):
        w, h = len(self.immagine[0]), len(self.immagine)
        for j in range(h):
            for i in range(w):
                d = abs(self.get_pixel(x, x) - x) + abs(self.get_pixel(y, y) - y) + abs(self.immagine[j][i][0] - self.immagine[y][x][0]) + abs(self.immagine[j][i][1] - self.immagine[y][x][1]) + abs(self.immagine[j][i][2] - self.immagine[y][x][2])
                self.immagine[j][i][3] = (d*self.immagine[j][i][3])/(d + t)

The variable d, I have to apply a formula made up as follows (here I get out of the error):

d = abs (xp - x) + abs (yp - y) + abs (rp - r) + abs (gp - g) + abs (bp - b)

where (xp, yp), (rp, gp, bp) op are, respectively, the position, color and the opacity of p, and (r, g, b) is the color of the pixel in position (x, y).

Traceback:

I tried manually, and when the program calls words.opacity(100, 160), the program returns the error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-3e2e34f8ee0f> in <module>()
----> 1 words.opacity(100, 160)

/home/user/Scrivania/python/program01.py in opacity(self, x, y, t)
155             for i in range(w):
156                 #d = abs(self.immagine[j][i].x - x) + abs(self.immagine[j][i].y - y) + abs(self.immagine[j][i][0] - self.immagine[y][x][0]) + abs(self.immagine[j][i][1] - self.immagine[y][x][1]) + abs(self.immagine[j][i][2] - self.immagine[y][x][2])
--> 157                 d = abs(self.get_pixel(x, x) - x) + abs(self.get_pixel(y, y) - y) + abs(self.immagine[j][i][0] - self.get_pixel[y][x][0]) + abs(self.immagine[j][i][1] - self.immagine[y][x][1]) + abs(self.immagine[j][i][2] - self.immagine[y][x][2])
158                 self.immagine[j][i][3] = (d*self.immagine[j][i][3])/(d + t)
159 

/home/user/Scrivania/python/program01.py in get_pixel(self, x, y)
145 
146     def get_pixel(self, x, y):
--> 147         if image.inside(x, y, self.immagine):
148             return self.immagine[y][x]
149         else:

/home/user/Scrivania/python/image.pyc in inside(img, i, j)
 78     '''Ritorna True se il pixel (i, j) e' dentro l'immagine img, False
 79     altrimenti'''
---> 80     w, h = len(img[0]), len(img)
 81     return 0 <= i < w and 0 <= j < h
 82 

TypeError: 'int' object has no attribute '__getitem__' 

Where the function inside of the image.pyc packet is:

def inside(img, i, j):
w, h = len(img[0]), len(img)
return 0 <= i < w and 0 <= j < h
1

There are 1 answers

2
Schnouki On

2 problems:

  • get_pixel() returns a Pixel instance, but you try to substract that with an int. So when you do self.get_pixel(x, x) - x, this runs the Pixel.__sub__ method with other=x, where x is an integer. Therefore, when it tries to access other.r, it fails, as other is an integer rather than an object with the r attribute.
  • self.get_pixel[y][x][0] won't work: self.get_pixel is a method. You probably mean self.get_pixel(y, x).r.