为什么我得到一个AttributeError:__ getitem__

时间:2015-01-20 20:02:30

标签: python

我目前正试图让文件夹中的所有图片都是半透明的,我从列表中调用它们,我现在不知道该怎么办

def convertimg(directory = None):

    if directory == None:
       directory = os.getcwd() 

    # Create a new directory 'modified'
    new_directory = os.path.join(directory, 'modified')
    try:
        os.mkdir(new_directory)
    except OSError:
        pass # if the directory already exists, proceed  

    #load all the images
    image_list, file_list = get_imgs(directory)

    if directory == None:
       directory = os.getcwd() 
    image_list, file_list = get_imgs(directory)
    for n in range(len(image_list)):

        im = image_list[n]
        im = im.convert("RGBA")
        for row in range(0,420):
            for column in range(0,500):
               im[row][column][3] = 127
        new_image = im
        filename, filetype = file_list[n].split('.')
        #save the altered image, suing PNG to retain transparency
        new_image_filename = os.path.join(new_directory, filename + '.png')
        new_image.save(new_image_filename) 

错误是convertimg()

AttributeError                            Traceback (most recent call last)
<ipython-input-32-0271d1c74903> in <module>()
----> 1 convertimg()

H:\Documents\GitHub\1_4_7\version2.py in convertimg(directory)
     47         for row in range(0,420):
     48             for column in range(0,500):
---> 49                im[row][column][3] = 127
     50         new_image = im
     51         filename, filetype = file_list[n].split('.')

C:\Users\i.levy\AppData\Local\Enthought\Canopy\User\lib\site-      packages\PIL\Image.pyc in __getattr__(self, name)
    510             new['data'] = self.tostring()
    511             return new
--> 512         raise AttributeError(name)
    513 
    514     ##

AttributeError: __getitem__ 

此代码块中出现错误

 im = image_list[n]
            im = im.convert("RGBA")
            for row in range(0,420):
                for column in range(0,500):
                   im[row][column][3] = 127

在最后一行。

1 个答案:

答案 0 :(得分:0)

不要将rowcolumn分别作为im的索引提供,请尝试将它们作为单个索引加入。

        im = im.convert("RGBA")
        for row in range(0,420):
            for column in range(0,500):
               im[row, column][3] = 127