How to read & export certain files from a Python GUI-prompted directory?
Question: OK guys,

I'm currently working on a file reading and processing with Python & OpenCV
cs' GUI feature. The feature will prompt the user to select a directory path
for a folder containing 340 JPEG images, which I labelled them as "frame1" to
"frame340". Then, I want to select several frames, process them, and save the
processed ones in a different directory.

My big issue is, I'm trying to get only frame87, frame164, and frame248 from
this folder with 340 images, and Python just keep returning error that claimed
"directory name is invalid", like this:

    
    
    Traceback (most recent call last):
     File "C:\Users\maxwell_hamzah\Documents\Python27\imageReadBeta.py", line 25, in <module>
      imgRead = os.listdir(str(dirname) + "/frame"+ str(i) + ".jpg")
    WindowsError: [Error 267] The directory name is invalid: 'C:/Users/maxwell_hamzah/Documents/Python27/toby arm framed/frame87.jpg/*.*'
    

To help familiarize with the situation, here's what my work looks like:

    
    
    import os
    import numpy as np
    import cv2
    from matplotlib import pyplot as plt
    from skimage import color, data, restoration
    import Tkinter, tkFileDialog
    
    # first, we setup the Tkinter features for file-reading
    root = Tkinter.Tk()
    root.withdraw()
    
    # prompt user to ask about the file directory
    dirname = tkFileDialog.askdirectory\
    (parent=root,initialdir="/",title='Pick FRAMES directory')
    
    X = [] # initiate an array to store read images
    frameIndex = [87, 163, 248] #this index is which frames we are interested in
    imgRead = ""
    temp = []
    
    
    # we begin to read only frame87, frame163, and frame248
    for i in frameIndex:
       imgRead = os.listdir(str(dirname) + "/frame"+ str(i) + ".jpg")
       temp = cv2.imread(imgRead, -1)
       X.append(temp)
    

I'm totally stuck on how to fix this bug on especially the for loop part,
where the error comes from. Python keeps freeking out on the imgRead variable
claiming that the directory is invalid. Plus, I'm also wondering on how to
"export" processed files to other directories (e.g. saving processed images
from "My Pictures" to "My Music")

Really appreciate your help, guys.

Maxwell


Answer: In the last block, you call a method to list files, which is expecting a
directory, but you pass it a file path. That's a bug, and actually you don't
need that here in the first place:

    
    
    for i in frameIndex:
       imgRead = "{0}/frame{1}.jpg".format(dirname, i)
       temp = cv2.imread(imgRead, -1)
       X.append(temp)
    

As to moving files in Python, that's a pretty classic need, there's plenty of
doc out there. [One
example](http://stackoverflow.com/questions/8858008/moving-a-file-in-python).

