Python UnboundLocalError:局部变量';文件列表';分配前参考

Python UnboundLocalError:局部变量';文件列表';分配前参考,python,Python,更新: 我以前放过图像列表和文件列表,没有问题,但现在它说AttributeError:“function”对象没有属性“save”,下面是新代码: 导入PIL 将matplotlib.pyplot作为plt导入 导入操作系统路径 导入PIL.ImageDraw def get_图像(目录=无): 图像_列表=[] 文件列表=[] 如果目录==无: directory=os.getcwd() 目录\u list=os.listdir(目录) 对于目录列表中的条目: 绝对文件名=os.path.

更新: 我以前放过图像列表和文件列表,没有问题,但现在它说AttributeError:“function”对象没有属性“save”,下面是新代码:

导入PIL
将matplotlib.pyplot作为plt导入
导入操作系统路径
导入PIL.ImageDraw
def get_图像(目录=无):
图像_列表=[]
文件列表=[]
如果目录==无:
directory=os.getcwd()
目录\u list=os.listdir(目录)
对于目录列表中的条目:
绝对文件名=os.path.join(目录,条目)
尝试:
image=PIL.image.open(绝对文件名)
文件列表+=[条目]
图像\u列表+=[图像]
除IOError外:
通过
返回图像列表、文件列表
def alter_one_映像(目录=无):
directory=os.path.dirname(os.path.abspath(_文件__))
chungus=os.path.join(目录'big_chungus.jfif')
chungus\u img=PIL.Image.open(chungus)
chungus_small=chungus_img.resize((89,87))
chungus_img.粘贴(chungus_small,(1162966),mask=chungus_small)
new=PIL.Image.new
还新
def alter_all_图像(目录=无):
如果目录==无:
directory=os.getcwd()
new_directory=os.path.join(目录,'modified')
尝试:
os.mkdir(新目录)
除操作错误外:
通过
图像列表,文件列表=获取图像(目录)
对于范围内的n(len(图像列表)):
印刷品
文件名,文件类型=文件列表[n]。拆分('.'))
当前图像=图像列表[n]
新建图像=更改一个图像(当前图像)
new_image_filename=os.path.join(new_目录,filename+'.png')
new_image.save(new_image_filename)
get_images()不会初始化“file_list”和“image_list”的空列表(如果directory等于None)。只需在函数开始时初始化它们,就可以解决问题。像这样:

def get_images(directory=None):
    image_list = [] 
    file_list = []
    if directory == None:
        directory = os.getcwd() 

请发布所获得错误的完整堆栈跟踪。请包含完整的错误回溯,我们不想猜测错误发生的时间点。请将错误回溯发布到
get\u images()
,如果
目录
,则
图像列表
不会初始化为空列表。在
获取图像()
中,如果
目录
不是
,则
图像列表
不会初始化为空列表。