如何完成此python函数以保存在同一文件夹中?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何完成此python函数以保存在同一文件夹中?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2146字,纯文字阅读大概需要4分钟。
内容图文
![如何完成此python函数以保存在同一文件夹中?](/upload/InfoBanner/zyjiaocheng/687/ff185b0f29094a79acbdac6f1117e7f6.jpg)
我试图写我的第一个真正的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保存在我加载图像时所在的目录中?而且可能有些东西遗失或错误,有人知道了吗?
相关问题:How to generate a filmstrip image in python from a folder of images?
解决方法:
它不是您问题的答案,但可能会有所帮助:
#!/usr/bin/env python
import Image
def makefilmstrip(images, mode='RGB', color='white'):
"""Return a combined (filmstripped, each on top of the other) image of the images.
"""
width = max(img.size[0] for img in images)
height = sum(img.size[1] for img in images)
image = Image.new(mode, (width, height), color)
left, upper = 0, 0
for img in images:
image.paste(img, (left, upper))
upper += img.size[1]
return image
if __name__=='__main__':
# Here's how it could be used:
from glob import glob
from optparse import OptionParser
# process command-line args
parser = OptionParser()
parser.add_option("-o", "--output", dest="file",
help="write combined image to OUTPUT")
options, filepatterns = parser.parse_args()
outfilename = options.file
filenames = []
for files in map(glob, filepatterns):
if files:
filenames += files
# construct image
images = map(Image.open, filenames)
img = makefilmstrip(images)
img.save(outfilename)
例:
$python filmstrip.py -o output.jpg *.jpg
内容总结
以上是互联网集市为您收集整理的如何完成此python函数以保存在同一文件夹中?全部内容,希望文章能够帮你解决如何完成此python函数以保存在同一文件夹中?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。