我正在尝试编写我的第一个真正的 python 函数来做一些真正的事情。我想要完成的是搜索给定的文件夹,然后打开所有图像并将它们合并在一起,以便它们制作幻灯片图像。想象一下 5 张图像堆叠在一张图像中。
我现在有这段代码,应该没问题,但可能需要一些修改:
import os
import Image
def filmstripOfImages():
imgpath = '/path/here/'
files = glob.glob(imgpath + '*.jpg')
imgwidth = files[0].size[0]
imgheight = files[0].size[1]
totalheight = imgheight * len(files)
filename = 'filmstrip.jpg'
filmstrip_url = imgpath + filename
# Create the new image. The background doesn't have to be white
white = (255,255,255)
filmtripimage = Image.new('RGB',(imgwidth, totalheight),white)
row = 0
for file in files:
img = Image.open(file)
left = 0
right = left + imgwidth
upper = row*imgheight
lower = upper + imgheight
box = (left,upper,right,lower)
row += 1
filmstripimage.paste(img, box)
try:
filmstripimage.save(filename, 'jpg', quality=90, optimize=1)
except:
filmstripimage.save(miniature_filename, 'jpg', quality=90)")
如何修改它,以便将新的filmstrip.jpg 保存在我从中加载图像的同一目录中?它可能有一些遗漏或错误的东西,有人知道吗?