for循环导致代码崩溃:PIL

问题描述 投票:0回答:3
import PIL   
import os.path  
from PIL import Image

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

    image_list = [] 
    file_list = []

    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 frame_image(image):

    family = Image.open(image)
    width, height = family.size
    familysize = width, height
    frame = Image.open('border.png')
    resizedborder = frame.resize(familysize)
    family.paste(resizedborder, mask=resizedborder)
    return family

def frame_all_images():
    directory = os.getcwd()

    new_directory = os.path.join(directory, "Framed Images")
    try:
        os.mkdir(new_directory)
    except:
        pass
    images, files = get_images(directory)
    for n in range(len(images)):
        new_image = frame_image(images[n])
        new_name = os.path.join(new_directory, files + ".png")
        new_image.save(new_name)
    print 'Success! All images have been framed!'

Picture of error given.

崩溃。我真的无法弄明白。当我指定一张图片时工作,但是当我使用“frame_all_images”函数给它一个图像列表时崩溃。我想这是获取图像的方式?提前致谢。

python python-2.7
3个回答
1
投票

您正在迭代Image对象而不是Image文件或路径。

def frame_all_images():
    directory = os.getcwd()

    new_directory = os.path.join(directory, "Framed Images")
    try:
        os.mkdir(new_directory)
    except:
        pass
    images, files = get_images(directory)
    for n in range(len(files)):
        new_image = frame_image(files[n])
        new_name = os.path.join(new_directory, files + ".png")
        new_image.save(new_name)
    print 'Success! All images have been framed!'

1
投票

您可以使用get_images获取图像和文件。请迭代文件以获取文件和图像以获取图像对象。

import PIL
import os.path
from PIL import Image


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

    image_list = []
    file_list = []

    directory_list = os.listdir(directory)
    for entry in directory_list:
        absolute_filename = os.path.join(directory, entry)
        try:
            #print absolute_filename
            if ".png"  in absolute_filename:
                image = PIL.Image.open(absolute_filename)
                file_list += [entry]
                image_list += [image]
        except IOError:
            pass
    return image_list, file_list


def frame_image(image):
    family = Image.open(image)
    width, height = family.size
    familysize = width, height
    frame = Image.open('border.png')
    resizedborder = frame.resize(familysize)
    family.paste(resizedborder, mask=resizedborder)
    return family


def frame_all_images():
    directory = os.getcwd()

    new_directory = os.path.join(directory, "Framed Images")
    try:
        os.mkdir(new_directory)
    except:
        pass
    images, files = get_images(directory)
    for n in range(len(files)):
        new_image = frame_image(files[n])
        new_name = os.path.join(new_directory, files + ".png")
        new_image.save(new_name)
    print 'Success! All images have been framed!'

def main():
    frame_all_images()

if __name__== "__main__":
  main()

1
投票

frame_image函数使用字符串路径而不是'PngImageFile'对象。 get_images函数打开路径并将'PngImageFile'附加到图像变量,因此在frame_all_images函数中,您尝试打开已打开的文件。

以下更改将解决此问题:

def frame_all_images():
    directory = os.getcwd()

    new_directory = os.path.join(directory, "Framed Images")
    try:
        os.mkdir(new_directory)
    except:
        pass

    images, files = get_images(directory)
    for n in range(len(images)):
        new_image = frame_image(files[n])
        new_name = os.path.join(new_directory, files[n] + ".png")
        new_image.save(new_name)
    print('Success! All images have been framed!')
© www.soinside.com 2019 - 2024. All rights reserved.