简体   繁体   中英

Python 3.5.1 - Assign variables to all Files (Filetype: .jpg) inside target folder

I am fairly new to Python and have a difficult Problem to solve:

Here is what I am trying to do -->

  1. I have a Folder (path known) with 1 - x picture files (.jpg) in it, whereas x can be as high as 1000
  2. These picture files should be stiched together (one on top of the other)
  3. To do so I want Python to assign a variable to each Picture file in the known folder and then create a loop which stiches these variable-stored pictures together and outputs it as 1 picture (.jpg)

Here is what I have coded so far:

from PIL import Image
import glob

#The following GLOB part doesn't work, I tried to make a List with all the
#files (.jpg) inside the main directory
#image_list = []

#for filename in glob.glob('*.jpg'):
#    test = Image.open(filename)
#    image_list.append(test)

img1 = Image.open("img1.jpg")
img2 = Image.open("img2.jpg")

def merge_images(img1, img2):

    (width1, height1) = img1.size
    (width2, height2) = img2.size
    result_width = max(width1, width2)
    result_height = height1 + height2

    result = Image.new('RGB', (result_width, result_height))
    result.paste(im=img2, box=(0,0))
    result.paste(im=img1, box=(0,height1-2890))

    return result

merged = merge_images(img1, img2)
merged.save("test.jpg") 

What this does is to assign img1.jpg and img2.jpg to a variable and then stack them on top of oneanother and save this stacked picture as "test.jpg". Where do I go from here if I want to assign many pictures (.jpg) to variables and stack them on top of each other without typing a line of code for each new picture (see description further up?)

Thanks a lot for your help!

Chris

If you start with a 0x0 image, you can stack further images onto it like this:

stacked_img = Image.new('RGB', (0, 0))

for filename in glob.glob('*.jpg'):
    stacked_img = merge_images(stacked_img, Image.open(filename))

stacked_img.save('stacked.jpg')

You might also like to change the height1-2890 to height2 in the merge_images() function if you are trying to stack the second image below the first image, ie

result.paste(im=img1, box=(0,height2))

Why not use a container such as a list ?

images = [Image.open(img_name) for img_name in os.listdir(my_folder) if img_name.endswith('.jpg')
def merge_images(list_images):
    result_width = max(img.size[0] for img in images)
    result_height = sum(img.size[1] for img in images)
    result = Image.new('RGB', (result_width, result_height))
    for idx, img in enumerate(list_images):
        result.paste(im=img, box=(0,list_images[0].size[1]-idx*2890))

See Data Structures for more information.

If performance is important, consider using map instead of a loop.

this is my code in which I implemented your approach. As a result, I am getting an image where 2 of the 4 images are displayed and the rest of the image is black. In the source directory I have 4 images (img1.jpg - img4.jpg)

from PIL import Image
import glob

stacked_img = Image.new('RGB', (0,0))


def merge_images(img1, img2):

    (width1, height1) = img1.size
    (width2, height2) = img2.size
    result_width = max(width1, width2)
    result_height = height1 + height2

    result = Image.new('RGB', (result_width, result_height))
    result.paste(im=img2, box=(0,0))

    result.paste(im=img1, box=(0,height1))

    return result


for filename in glob.glob('*.jpg'):
    stacked_img = merge_images(stacked_img, Image.open(filename))

stacked_img.save('stacked.jpg')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM