Python 根据文件夹中的所有图像的文件名,按一定顺序循环浏览它们

Python 根据文件夹中的所有图像的文件名,按一定顺序循环浏览它们,python,Python,所以,我想创建一个实用工具来将图像缝合在一起。 到目前为止,我有以下代码: def merger(file1, file2): # File I/O: Open source images image1 = Image.open(file1) image2 = Image.open(file2) # Read the source image dimensions using pillow Image class (width1, height1) =

所以,我想创建一个实用工具来将图像缝合在一起。 到目前为止,我有以下代码:

def merger(file1, file2):

    # File I/O: Open source images
    image1 = Image.open(file1)
    image2 = Image.open(file2)

    # Read the source image dimensions using pillow Image class
    (width1, height1) = image1.size
    (width2, height2) = image2.size

    # Calculate the output image dimensions
    merged_width = width1 + width2          # The width is a sum of the 2 widths
    merged_height = max(height1, height2)   # The height is the larger of the two

    # Merge the images using pillow Image class
    output = Image.new('RGB', (merged_width, merged_height))   # Create new image object (for output)
    output.paste(im=image1, box=(0, 0))                        # Paste the 1st source image into the output object
    output.paste(im=image2, box=(width1, 0))                   # Paste the 2nd source image into the output object
    return output
如何循环浏览文件夹中的所有图像文件? 我想我会使用循环,但我如何读取文件夹中的每一对图像文件,从文件名中识别它们的顺序,将每一对缝合在一起,然后继续下一对文件

应根据文件名中的数字缝合文件。示例:

首先缝合
1.jpg
2.jpg
,然后缝合
3.jpg
4.jpg
5.jpg
6.jpg
等等

应先缝合
01.jpg
02.jpg
,然后缝合
03.jpg
04.jpg
05.jpg
06.jpg

scan01.tif
scan02.tif
,然后
scan03.tif
scan04.tif
scan05.tif
scan06.tif

首先是
newpic0001.png
newpic0002.png
,然后是
newpic0003.png
newpic0004.png
,然后是
newpic0005.png
newpic0006.png

你得到的想法是:根据文件末尾的数字,忽略前导的零

我使用枕头进行图像处理,如果有必要的话,使用tkinter进行GUI

谢谢。

试试这个:

import os

file_list = [x for x in sorted([x for x in os.listdir('/path/to/directory/')])]
for i in range (0, len(file_list), 2):
    if i+1 < len(file_list):
        f1, f2 = file_list[i], file_list[i+1]
    else:
        f1, f2 = file_list[i], None
    # do your merge magic here with f1 and f2
导入操作系统
file_list=[x代表已排序的x([x代表os.listdir('/path/to/directory/'))]]
对于范围内的i(0,len(文件列表),2):
如果i+1
参考资料:

这感觉有点野蛮,但确实有效。其思想是获取文件名列表,然后仅使用文件名中的数字对其进行排序

import os
import re


def sort_filenames(all_files):
    filenames_sorted = []
    original_filenames = {}
    for full_filename in all_files:
        filename, file_extension = os.path.splitext(full_filename)

        # Save all the files names to be sorted
        filenames_sorted.append(filename)
        # Save original full filename in a dictionary for later retrieval
        original_filenames[filename] = full_filename

    # Sort the list using our own key
    filenames_sorted.sort(key=get_file_key)
    filenames = []
    for key in filenames_sorted:
        filenames.append(original_filenames[key])

    return filenames


def get_file_key(filename):
    # Remove all non-digits from the filename
    key = re.sub("[^0-9]", "", filename)
    return int(key)


# Start with the list of all files in the current directory
all_files = os.listdir("./")

# FOR TESTING ONLY....fill all_files with some testing examples
all_files = ['scan01.tif', 'scan04.tif', 'scan03.tif', 'scan05.tif', 'scan06.tif', 'scan02.tif']
all_files = ['newpic0002.png', 'newpic0001.png', 'newpic0003.png', 'newpic0006.png', 'newpic0005.png', 'newpic0004.png']

sorted_files = sort_filenames(all_files)

for i in range(0, len(sorted_files) - 1, 2):
    # Just printing the statement to confirm we have it right
    print('merger({}, {})'.format(sorted_files[i], sorted_files[i + 1]))


注意:即
get\u file\u key()
从文件名中提取数字,并使用它们对数组进行排序。如果文件名中包含的数字不属于排序的一部分,则此处可能存在问题