我正在尝试编写我的第一个真正的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)")
我如何修改它以保存新的电影胶片.jpg在我加载图片的同一个目录中?它可能有一些东西丢失或错误,有人知道吗?在
相关问题:How to generate a filmstrip image in python from a folder of images?
如果您不是在开玩笑,您的脚本有几个问题,例如
glob.glob()
返回文件名列表(字符串对象,而不是图像对象),因此files[0].size[0]
将无法工作。在这不是对您问题的回答,但可能会有所帮助:
示例:
^{pr2}$我想如果你把你的
try
部分改成这样:相关问题 更多 >
编程相关推荐