簡體   English   中英

根據文件名以一定順序循環瀏覽文件夾中的所有圖像

[英]Loop through all the images in a folder in a certain order according to their file names

因此,我想創建一個將圖像拼接在一起的實用程序。 到目前為止,我有以下代碼:

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.jpg2.jpg應該第一縫制,然后3.jpg4.jpg5.jpg6.jpg等。

要么

應該先縫合01.jpg02.jpg ,然后再縫合03.jpg04.jpg05.jpg06.jpg等等。

要么

scan01.tifscan02.tif ,然后scan03.tifscan04.tifscan05.tifscan06.tif ...

要么

newpic0001.pngnewpic0002.png ,然后是newpic0003.pngnewpic0004.png ,然后是newpic0005.pngnewpic0006.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

參考: 列出路徑中的所有目錄

這感覺有些殘酷,但是有效。 想法是獲取文件名列表,然后僅使用文件名中的數字對它們進行排序。

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_file_key()從文件名中提取數字,並使用它們對數組進行排序。 如果文件名包含的數字不屬於排序范圍,則此處可能會出現問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM