【问题标题】:How can I iterate image processing through a directory of image files in a sub folder within Python?如何通过 Python 子文件夹中的图像文件目录迭代图像处理?
【发布时间】:2012-03-21 08:59:06
【问题描述】:

在如何连接迭代列表(声明为 fileListL)方面需要帮助,该列表是从子目录(左)中的一系列图像文件动态构建的,并循环直到到达帧范围的末尾。所有脚本都在单个帧上按预期工作,但我无法让它循环多个帧。想法,想法?

# this module runs within a root directory that contains one sub folder named 'left' and 15 sub folders expressed in the dirL as ('left/01','left/02, . . . .)
# each folder can have any number of sequential .jpg images in the format 'name.0000.jpg'

import os
import sys
import glob
import Image
from itertools import izip, count

# create Master List of files in Left Directories along with a count of files

global fileListL
fileListL = []
for root, dirs, files in os.walk(r'left/'):
    for file in files:
        if file.endswith('.jpg'):
                fileListL.append('left/'+file)
print fileListL

# Iterator
global inFrame
global outFrame
inFrame = 'left/test.0000.jpg' # testing temp needs dynamic variable, trying to use fileList L list to dynamically increment thru frames
outFrame = inFrame[5:-5]
crops = ((0, 0, 1920, 1080),(1920, 0, 3840, 1080), (3840, 0, 5760, 1080), (5760, 0, 7680, 1080), (7680, 0, 9600, 1080), (9600, 0, 11520, 1080), (11520, 0, 13440, 1080), (13440, 0, 15360, 1080), (15360, 0, 17280, 1080), (17280, 0, 19200, 1080), (19200, 0, 21120, 1080), (21120, 0, 23040, 1080), (23040, 0, 24960, 1080), (24960, 0, 26880, 1080), (26880, 0, 28800, 1080))
quads = ('01_', '02_', '03_', '04_', '05_', '06_', '07_', '08_', '09_', '10_', '11_', '12_', '13_', '14_', '15_')
dirL= ('left/01/', 'left/02/', 'left/03/', 'left/04/', 'left/05/', 'left/06/', 'left/07/', 'left/08/', 'left/09/', 'left/10/', 'left/11/', 'left/12/', 'left/13/', 'left/14/', 'left/15/')

for i in fileListL:
    for infile in glob.glob( os.path.join(inFrame) ):
        print "current file is: " + infile
        oL = Image.open(inFrame) # needs dynamic variable for frames
        for i, each_quad, each_frame, each_dirL in izip(count(), crops, quads, dirL):
            print i, each_quad, each_frame, each_dirL
            frame = oL.crop(((each_quad)))
            frame.save((each_dirL)+(each_frame)+(outFrame)+'.png')

【问题讨论】:

  • 你有什么错误吗?消息打印正常吗?
  • 在分配给“inFrame”的单个帧上运行模块时没有错误。每当我在左根目录中有多个图像时,它只会覆盖所有子目录中的相同分割图像. 所以本身没有错误,只需要得到一个正确循环通过 fileListL 的循环。
  • Image.open(inFrame) 你是否总是打算打开同一张图片?
  • No (inFrame) 当前设置为静态值,以便更容易解释问题。理想情况下,fileListL 需要迭代地输入这个值。想知道我是否需要对所有内容进行一些重组以以不同的方式滑动帧序列?

标签: python list iterator directory


【解决方案1】:

您的代码中有很多“复制和粘贴”错误,难以理解其目的。

  • 嵌套的for 循环使用同名的循环变量 (i)。
  • os.path.join(inFrame) 需要一个包含路径组件的列表,inFrame 似乎是一个字符串
  • 第一个 for 循环 (for i in fileListL) 的目的是什么,好像您正在丢弃 i
  • 等等……

请使用您的文件树布局和适用于单个文件的步骤更新问题,也许我们可以帮助您。

[更新]

似乎您的代码可以简化为:

for i, file_name in enumerate(glob.glob('left/*.jpg')):
    out_frame = 'left/%02d/%s.png' % (i+1, file_name[5:-5])
    print "current input file is: '%s', output file is: %s" % (file_name, out_frame)
    img = Image.open(file_name)
    for i, crop in enumerate(crops):
        frame = img.crop(crop)
        frame.save(out_frame)

这是未经测试的代码,只是为了给你指明正确的方向。

【讨论】:

  • 这在包含一个名为“left”的子文件夹的根目录中运行 - 每个文件夹可以有任意数量的连续 .jpg 图像,格式为“name.0000.jpg”,它将单个图像分成 15 个裁剪和重命名的子图像,并将它们分配到 15 个多个子文件夹中。希望有帮助。 . .
  • 嗯。 .收到错误:out_frame = 'left/%02d/%s.png' % (i+1, filename[5:-5]) TypeError: cannot concatenate 'str' and 'int' objects
  • 尝试添加 str(filename) 并调用单独的变量,但得到相同的错误
  • @ROcketsLAsersandRObots:抱歉,代码缺少“枚举”来填充循环变量i
  • 还是不行。这是新的错误“AttributeError: 'list' object has no attribute 'read'”
猜你喜欢
  • 1970-01-01
  • 2016-09-22
  • 1970-01-01
  • 2023-02-20
  • 2010-09-28
  • 2018-08-30
  • 2022-10-07
  • 2017-01-24
  • 1970-01-01
相关资源
最近更新 更多