【问题标题】:How do I read the pictures in the file in order? [closed]如何按顺序读取文件中的图片? [关闭]
【发布时间】:2022-12-01 03:04:24
【问题描述】:

我想按照文件中的顺序读取文件中的图片。但是当我用 python 阅读它时,它看起来很复杂。我不希望它排序。我怎样才能解决这个问题?

def read_img(path):
    
    st = os.path.join(path, "*.JPG")
    
    st_ = os.path.join(path, "*.jpg")
     
    for filename in glob.glob(st):
        print(st)
        #print("filename-------",filename)
        
        img_array_input.append(filename)
        print("image array append : ", filename)
        
    for filename in glob.glob(st_):
        
        img_array_input.append(filename)
        #print("filename-------",filename)
        global size
        size = len(img_array_input)
        
    for i in img_array_input:   

        print("detection ")     
        
        detection(i)
        print("detection out") 

enter image description here

原始文件

enter image description here

阅读顺序

我希望它按原始文件中的顺序读取。

【问题讨论】:

标签: python image readfile


【解决方案1】:

如果您希望列表按以下顺序填充os.listdir()然后显示文件:

from os import listdir
from os.path import join, splitext

BASE = '.' # directory to be parsed
EXTS = {'jpg', 'JPG', 'jpeg', 'JPEG'} # file extensions of interest

def ext(p):
    _, ext = splitext(p)
    if ext:
        return ext[1:]

def getfiles(base, include_base=True):
    for entry in listdir(base):
        if ext(entry) in EXTS:
            yield join(base, entry) if include_base else entry

detection = [file for file in getfiles(BASE, include_base=False)]

print(detection)

笔记:

os.listdir()以任意顺序返回文件列表

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 2014-11-03
    相关资源
    最近更新 更多