<span class = "cookie">  Cookie  </span>
崩溃。我真的无法解决这个问题。当我指定一张图片时工作,但是当我使用&#34; frame_all_images&#34;时,当我给它一个图像列表时崩溃。功能。我想象它是获取图像的方式吗?提前谢谢。
答案 0 :(得分:1)
您正在迭代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 :(得分: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()
答案 2 :(得分: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!')