Python - 'int'对象没有属性'__getitem__'

时间:2022-03-16 08:03:56

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)

我需要创建一个类像素(这是我创建的没有问题)和一个类PImage,我发现这个问题:(只带最重要的方法)

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

类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,我需要应用一个公式,如下所示(这里我从错误中得到):

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

式中(xp, yp), (rp, gp, bp) op分别为p的位置、颜色和不透明度,(r, g, b)为位置(x, y)像素的颜色。

Traceback:

回溯:

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

我手动尝试,当程序调用文字。不透明度(100,160),程序返回错误

---------------------------------------------------------------------------
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 个解决方案

#1


1  

2 problems:

2个问题:

  • 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.
  • get_pixel()返回一个像素实例,但是您尝试使用int来进行细分。get_pixel(x, x) - x,运行该像素。__sub__与other=x一起的方法,其中x是一个整数。因此,当它试图访问其他。它失败了,因为其他是整数而不是具有r属性的对象。
  • self.get_pixel[y][x][0] won't work: self.get_pixel is a method. You probably mean self.get_pixel(y, x).r.
  • 自我。get_pixel[y][x][0]行不通:自我。get_pixel是一个方法。你可能意味着自我。get_pixel(y、x)r。

#1


1  

2 problems:

2个问题:

  • 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.
  • get_pixel()返回一个像素实例,但是您尝试使用int来进行细分。get_pixel(x, x) - x,运行该像素。__sub__与other=x一起的方法,其中x是一个整数。因此,当它试图访问其他。它失败了,因为其他是整数而不是具有r属性的对象。
  • self.get_pixel[y][x][0] won't work: self.get_pixel is a method. You probably mean self.get_pixel(y, x).r.
  • 自我。get_pixel[y][x][0]行不通:自我。get_pixel是一个方法。你可能意味着自我。get_pixel(y、x)r。