python写完程序保存,如何完成此python函数以保存在同一个文件夹中?

I am trying to write my first real python function that does something real. What i want to accomplish is searching a given folder, and then open all images and merging them together so they make a filmstrip image. Imagine 5 images stacked on top of eachother in one image.

I have this code now, which should be pretty much ok, but propably needs some modification:

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)")

How do i modify this so that it saves the new filmstrip.jpg in the same directory as I loaded the images from? And it probably has some things that are missing or wrong, anybody got a clue?

解决方案

It is not an answer to your question, but It might be helpful:

#!/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)

Example:

$ python filmstrip.py -o output.jpg *.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值