AttributeError:'ResizeImage'对象没有属性'thumbnail'

时间:2018-04-02 19:05:43

标签: python class

我正在上课,为照片制作更好的“缩略图”。 这些函数运行正常,但在类中我得到的“对象”没有“属性'缩略图'”。我不是课程专家,但也许是一个简短的推荐? “开放”方法运行良好!

class ResizeImage:
    from PIL import Image
    def newImage(self,dimensiune):
        NouaPoza = Image.new('RGBA', (dimensiune, dimensiune), (255, 255, 255, 0))
        self.thumbnail((dimensiune, dimensiune), Image.ANTIALIAS)
        coordonateCentrare = ((dimensiune - self.size[0]) // 2, (dimensiune - self.size[1]) // 2)
        NouaPoza.paste(self,coordonateCentrare)
        return NouaPoza
    def openVechi(self,fisier_in):
        self = Image.open(fisier_in)
        return self
    def saveNou(self,fisier_out):
        self.save(fisier_out)
if __name__ == '__main__':
    fisier_in = "[...]"
    fisier_out = "[...]"
    poza = ResizeImage()
    poza.openVechi(fisier_in)
    poza.newImage(500)
    poza.saveNou(fisier_out)

提前谢谢!

P.S。只使用函数是可以的,例如:     def thumbnail(poza,dimensiune):         poza.thumbnail((dimensiune,dimensiune),Image.ANTIALIAS)

EDIT 我认为正确的声明是:

class ResizeImage(Image.Image):
    def newImage(self,dimensiune):
        self.thumbnail((dimensiune,dimensiune),Image.ANTIALIAS)

但是我收到以下错误:

文件“C:/Users/claudiu.ivanescu/PycharmProjects/eDX/NewImage.py”,第11行,in newImage     self.thumbnail((dimensiune,dimensiune),Image.ANTIALIAS)   文件“C:\ Users \ claudiu.ivanescu \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ PIL \ Image.py”,第2059行,缩略图     x,y = self.size AttributeError:'ResizeImage'对象没有属性'size'

1 个答案:

答案 0 :(得分:0)

经过一番研究后,我放弃了以前的课程。我把所有细节都作为对象的属性。而所有其他方法我都没有args访问它们。

有新课程:

class resizeImage:
    def __init__(self,dimension,fileIn,fileOut)
        [...]
    def createThumbnail(self):
        from PIL import Image
        picture = Image.open(self.fileIn)
        [...]
        thumbnail.save(self.fileOut)
    def delThumbnail(self):
        import os,sys
        os.remove(self.fileOut)

也许对某些人有用!