How can I iterate image processing through a directory of image files in a sub folder within Python?
Question: Needing help specifically with how to connect an iterated list (declared as
fileListL) which is dynamically built from a sequence of image files within a
sub directory (left) and loops through until it reaches the end of the frame
range. All of the script works as intended on a single frame but I am not able
to get this to loop through multiple frames. Ideas, thoughts?

    
    
    # 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')
    


Answer: There are so many "copy&paste" mistakes in your code that is hard to grasp
it's purpose.

  * nested `for` loops using the same name for loop variable (`i`).
  * `os.path.join(inFrame)` expects a list containing path components, `inFrame` seems to be a string
  * what is the purpose of the first for loop (`for i in fileListL`), seems like you are discarding `i`.
  * and so on...

Please update the question with your file tree layout and the steps that work
for a single file and perhaps we may help you.

[update]

Seems like your code could be simplified to something like:

    
    
    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)
    

It's untested code just to point you the right direction.

