我如何按顺序阅读图像?

问题描述 投票:0回答:1

我在目录中有图像,图像文件名为:a1,a2,a3,... a900

我想基于图像名称按顺序读取图像并进行一些处理。但问题是它随机读取图像,而不是顺序读取。

为了解决此问题,我基于natsorted编写了以下程序。但是,它不能正确执行工作。

def load_images(path):
    image_list=[]
    images= glob.glob(path)
    images = natsorted(images)

    for index in range(len(images)):
        image= cv2.cvtColor(cv2.imread(images[index]),cv2.COLOR_BGR2RGB)
        image_list.append(cv2.resize(image,(1920,1080)))

    return image_list

如何解决此问题?

python image image-processing imread
1个回答
0
投票

没有问题。natsort程序包运行正常。

>>> from natsort import natsorted
>>> imgs = [f'a{i}' for i in range(12, 0, -1)]
>>> 
>>> sorted(imgs)
['a1', 'a10', 'a11', 'a12', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9']
>>> 
>>> natsorted(imgs)
['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10', 'a11', 'a12']
>>> 
>>> imgs
['a12', 'a11', 'a10', 'a9', 'a8', 'a7', 'a6', 'a5', 'a4', 'a3', 'a2', 'a1']
© www.soinside.com 2019 - 2024. All rights reserved.