简体   繁体   English

对文件夹中的 40k 图像进行排序 python

[英]Sort 40k images in a folder python

import os
import time
from PIL import Image as PImage
import pathlib 
import glob

try:
    path = r"\\x.x.x.x\PVCTData\ELImages\2021_03_08"
    os.chdir(path)
    combo = os.listdir(path)
    combo.sort(key=os.path.getctime,reverse=True)
    print("done")
    print(combo)
    x=0
    loadedimages=[]
 
    for image in combo:
            print(image)
            img = PImage.open(os.path.join(path,image))
            loadedimages.append(img)
            x+=1
            if x>3:
                break
    print(loadedimages)
    loadedimages[0].show()
    loadedimages[1].show()
    loadedimages[2].show()
except Exception as ex:
    print(ex)

Assuming i have a shared folder with 40k pictures.假设我有一个包含 40k 图片的共享文件夹。 I would like to sort the pictures based on the creation date as my machine will send new picture to the folder every 5 seconds.我想根据创建日期对图片进行排序,因为我的机器每 5 秒将新图片发送到文件夹。 The code above can work but it is too slow to handle the amount of pictures which it took about 15 mins in order to sort and show.上面的代码可以工作,但它太慢了,无法处理需要大约 15 分钟才能进行排序和显示的图片数量。 I only need to show the latest 60 pictures every 1 hour.我只需要每 1 小时显示最新的 60 张图片。

You have to filter the file list before sorting, such that any file you don't care about is not sorted.您必须在排序之前过滤文件列表,这样您不关心的任何文件都不会被排序。 I recommend you move the files you don't need to another location (organized properly is a plus).我建议您将不需要的文件移动到另一个位置(组织得当是一个优势)。

To do so use something like this before sorting:为此,请在排序之前使用类似的内容:

combo = os.listdir()
currentTime = time.time()
timerange = 310
oldestOK = currentTime - timerange
filtered = list()
for file in os.listdir():
    if path.getctime(file) > oldestOK:
        filtered.append(file)

filtered.sort(key=os.path.getctime, reverse=True)

If you only need to use the last 60 items, why not just only loop over that part of the sort?如果您只需要使用最后 60 个项目,为什么不只遍历排序的那一部分呢?
So instead of所以而不是

for image in combo:
for image in combo[:60]:

Of course I'm not sure which part takes longer (the sorting, or handling the images after they are sorted).当然,我不确定哪个部分需要更长的时间(排序,或排序后处理图像)。
And ofcourse what Christian stated, try to order them in more subdirectories after you've managed them.当然,克里斯蒂安所说的,在你管理它们之后,试着在更多的子目录中订购它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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