UnboundLocalError:分配前已引用局部变量“ file_list”

时间:2019-02-22 17:06:12

标签: python

更新: 我把image_list和file_list放在前面,但我没有问题,但是现在它说AttributeError:'function'对象没有属性'save',这是新代码:

import PIL
import matplotlib.pyplot as plt 
import os.path  
import PIL.ImageDraw 

def get_images(directory=None):
    image_list = [] 
    file_list = []
    if directory == None:
        directory = os.getcwd() 
    directory_list = os.listdir(directory) 
    for entry in directory_list:
        absolute_filename = os.path.join(directory, entry)
        try: 
            image = PIL.Image.open(absolute_filename)
            file_list+= [entry]
            image_list+=[image]
        except IOError:
            pass 
    return image_list, file_list

def alter_one_image(directory=None):
    directory = os.path.dirname(os.path.abspath(__file__))  
    chungus = os.path.join(directory, 'big_chungus.jfif')
    chungus_img = PIL.Image.open(chungus)
    chungus_small = chungus_img.resize((89, 87))
    chungus_img.paste(chungus_small, (1162, 966), mask=chungus_small)
    new=PIL.Image.new
    return new
def alter_all_images(directory=None):
    if directory == None:
        directory = os.getcwd()
        new_directory = os.path.join(directory,'modified')
        try:
            os.mkdir(new_directory)
        except OSError:
            pass
            image_list, file_list = get_images(directory)
            for n in range (len(image_list)):
                print n
                filename,filetype = file_list[n].split('.')
                curr_image = image_list[n]
                new_image = alter_one_image(curr_image)
                new_image_filename = os.path.join(new_directory, filename+'.png')
                new_image.save(new_image_filename)
这是完整的回溯错误:

alter_all_images()

0

AttributeErrorTraceback(最近一次通话最近)  在()中 ----> 1个alter_all_images()

alter_all_images(目录)中的C:\ Users \ terrazg20 \ Documents \ GabeTerrazas_1_4_5AlterAllImages.py      43 new_image = alter_one_image(curr_image)      44 new_image_filename = os.path.join(new_directory,filename +'。png') ---> 45个new_image.save(new_image_filename)

AttributeError:“函数”对象没有属性“保存”

1 个答案:

答案 0 :(得分:1)

如果目录等于“无”,则

get_images()不会初始化“ file_list”和“ image_list”的空列表。只需在函数开始时对其进行初始化,即可解决问题。像这样:

`
var attributeValue = new AttributeValue().withS(value);
var expressionAttributeValueMap = Map.of(":v1", attributeValue);
var queryRequest = new QueryRequest("myTable")
  .withExpressionAttributeValues(expressionAttributeValeMap)
  .withKeyConditionExpression("myAttr = :v1");
`