【问题标题】:How to complete this python function to save in the same folder?如何完成这个python函数保存在同一个文件夹中?
【发布时间】:2010-09-25 01:17:57
【问题描述】:

我正在尝试编写我的第一个真正的 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?

【问题讨论】:

  • 您忘记导入 glob(或忘记粘贴导入语句)
  • 您忘记尝试运行代码至少一次

标签: python image-processing python-imaging-library


【解决方案1】:

我想如果您将 try 部分更改为:

filmstripimage.save(filmstrip_url, 'jpg', quality=90, optimize=1)

【讨论】:

    【解决方案2】:

    如果您不是在开玩笑,那么您的脚本有几个问题,例如glob.glob() 返回文件名列表(字符串对象,而不是图像对象),因此 files[0].size[0] 将不起作用。

    【讨论】:

      【解决方案3】:

      正如 J. F. Sebastian 提到的,glob 不返回图像对象......但也:

      就像现在一样,脚本假定文件夹中的图像大小和形状都相同。这通常不是一个安全的假设。

      因此,出于这两个原因,您需要先打开图像,然后才能确定它们的大小。一旦你打开它,你应该设置宽度,并将图像缩放到那个宽度,这样就没有空白了。

      另外,您没有在脚本中的任何位置设置 mini-filename。

      【讨论】:

        【解决方案4】:

        这不是您问题的答案,但可能会有所帮助:

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

        【讨论】:

        • 谢谢,这个解决方案看起来很聪明,但是当我尝试实现它并运行地图时(Image.open,...)我得到一个错误:“type object 'Image' has no attribute 'open'" 有什么线索可以解释为什么会这样吗?
        • @Espen Christensen:检查Image = type(Image) 是否出现在您的代码中。复制粘贴我的代码。结果如何?
        猜你喜欢
        • 1970-01-01
        • 2021-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-25
        • 2014-07-28
        相关资源
        最近更新 更多