如何以有效的方式列出图像序列

如何以有效的方式列出图像序列

本文介绍了如何以有效的方式列出图像序列? Python中的数字序列比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有9张图片的目录:


image_0001, image_0002, image_0003
image_0010, image_0011
image_0011-1, image_0011-2, image_0011-3
image_9999

我希望能够以一种有效的方式列出它们(像这样(9个图像有4个条目):

I would like to be able to list them in an efficient way, like this (4 entries for 9 images):


(image_000[1-3], image_00[10-11], image_0011-[1-3], image_9999)

python中是否有一种方法可以简短/清晰地返回图像目录(不列出每个文件)?

Is there a way in python, to return a directory of images, in a short/clear way (without listing every file)?

所以,可能是这样的:

列出所有图像,按数字排序,创建一个列表(从头开始按顺序计数每个图像).如果缺少图像(创建新列表),请继续操作,直到完成原始文件列表.现在,我应该有一些包含不间断序列的列表.

list all images, sort numerically, create a list (counting each image in sequence from start).When an image is missing (create a new list), continue until original file list is finished.Now I should just have some lists that contain non broken sequences.

我正在尝试使其易于阅读/描述数字列表.如果我有一个连续的1000个文件序列,则可以将其清楚地列为file [0001-1000],而不是file ['0001','0002','0003'等...]

I'm trying to make it easy to read/describe a list of numbers. If I had a sequence of 1000 consecutive files It could be clearly listed as file[0001-1000] rather than file['0001','0002','0003' etc...]

Edit1 (根据建议):给定一个扁平的列表,您将如何得出全局模式?

Edit1(based on suggestion): Given a flattened list, how would you derive the glob patterns?

Edit2 我正在尝试将问题分解为更小的部分.这是部分解决方案的示例:data1有效,data2返回0010为64,data3(真实世界的数据)不起作用:

Edit2 I'm trying to break the problem down into smaller pieces. Here is an example of part of the solution:data1 works, data2 returns 0010 as 64, data3 (the realworld data) doesn't work:

# Find runs of consecutive numbers using groupby.  The key to the solution
# is differencing with a range so that consecutive numbers all appear in
# same group.
from operator import itemgetter
from itertools import *

data1=[01,02,03,10,11,100,9999]
data2=[0001,0002,0003,0010,0011,0100,9999]
data3=['image_0001','image_0002','image_0003','image_0010','image_0011','image_0011-2','image_0011-3','image_0100','image_9999']

list1 = []
for k, g in groupby(enumerate(data1), lambda (i,x):i-x):
    list1.append(map(itemgetter(1), g))
print 'data1'
print list1

list2 = []
for k, g in groupby(enumerate(data2), lambda (i,x):i-x):
    list2.append(map(itemgetter(1), g))
print '\ndata2'
print list2

返回:

data1
[[1, 2, 3], [10, 11], [100], [9999]]

data2
[[1, 2, 3], [8, 9], [64], [9999]]

推荐答案

以下是您要实现的工作实现,以您添加的代码为起点:

Here is a working implementation of what you want to achieve, using the code you added as a starting point:

#!/usr/bin/env python

import itertools
import re

# This algorithm only works if DATA is sorted.
DATA = ["image_0001", "image_0002", "image_0003",
        "image_0010", "image_0011",
        "image_0011-1", "image_0011-2", "image_0011-3",
        "image_0100", "image_9999"]

def extract_number(name):
    # Match the last number in the name and return it as a string,
    # including leading zeroes (that's important for formatting below).
    return re.findall(r"\d+$", name)[0]

def collapse_group(group):
    if len(group) == 1:
        return group[0][1]  # Unique names collapse to themselves.
    first = extract_number(group[0][1])  # Fetch range
    last = extract_number(group[-1][1])  # of this group.
    # Cheap way to compute the string length of the upper bound,
    # discarding leading zeroes.
    length = len(str(int(last)))
    # Now we have the length of the variable part of the names,
    # the rest is only formatting.
    return "%s[%s-%s]" % (group[0][1][:-length],
        first[-length:], last[-length:])

groups = [collapse_group(tuple(group)) \
    for key, group in itertools.groupby(enumerate(DATA),
        lambda(index, name): index - int(extract_number(name)))]

print groups

这将打印出您想要的['image_000[1-3]', 'image_00[10-11]', 'image_0011-[1-3]', 'image_0100', 'image_9999'].

历史:我最初是向后回答问题的,正如@Mark Ransom在下面指出的那样.为了历史,我最初的答案是:

HISTORY: I initially answered the question backwards, as @Mark Ransom pointed out below. For the sake of history, my original answer was:

您正在寻找 glob .试试:

import glob
images = glob.glob("image_[0-9]*")

或者,使用您的示例:

images = [glob.glob(pattern) for pattern in ("image_000[1-3]*",
    "image_00[10-11]*", "image_0011-[1-3]*", "image_9999*")]
images = [image for seq in images for image in seq]  # flatten the list

这篇关于如何以有效的方式列出图像序列? Python中的数字序列比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 13:34