Skip to content Skip to sidebar Skip to footer

How To Complete This Python Function To Save In The Same Folder?

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 togeth

Solution 1:

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

#!/usr/bin/env pythonimport Image

defmakefilmstrip(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, 0for 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 inmap(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

Solution 2:

I think if you change your try section to this:

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

Solution 3:

In the case you are not joking there are several problems with your script e.g. glob.glob() returns list of filenames (string objects, not Image objects) therefore files[0].size[0] will not work.

Solution 4:

as J. F. Sebastian mentioned, glob does not return image objects... but also:

As it is right now, the script assumes the images in the folder are all the same size and shape. This is not often a safe assumption to make.

So for both of those reasons, you'll need to open the images before you can determine their size. Once you open it you should set the width, and scale the images to that width so there is no empty space.

Also, you didn't set miniature_filename anywhere in the script.

Post a Comment for "How To Complete This Python Function To Save In The Same Folder?"