Common ways of reading pictures in deep learning:
globe
Use it to find file pathnames that match certain rules. Only three matching characters are used to find files: "*", "?", "[]";
"*" matches 0 or more characters;
"?" matches a single character;
"[]" matches characters within the specified range, such as: [0-9] matches numbers;
glob.glob
Returns a list of all matching file paths. It has only one parameter pathname, which defines the file path matching rule, which can be an absolute path or a relative path.
import glob #Get all pictures in the specified directory print glob.glob(r"E:/Picture/*/*.jpg") #Get all .py files in the parent directory print glob.glob(r'../*.py') #relative path
glob.iglob
Gets a calendarable object that can be used to get matching file pathnames one by one. The difference from glob.glob() is: glob.glob gets all matching paths at the same time, while glob.iglob gets only one matching path at a time.import glob #.py file in parent directory f = glob.iglob(r'../*.py') print f #<generator object iglob at 0x00B9FF80> for py in f: print pyGet a list of all jpg image files in the images/training folder:
import them import glob image_dir = r"C:\Users\Administrator\Desktop"; file_glob = os.path.join(image_dir,"images","training","*."+"jpg") print(file_glob) file_list = [] file_list.extend(glob.glob(file_glob)) print(file_list)结果如下:
C:\Users\Administrator\Desktop\images\training\*.jpg
['C:\\Users\\Administrator\\Desktop\\images\\training\\TJ_3.JPG', 'C:\\Users\\Ad
ministrator\\Desktop\\images\\training\\TJ_S.JPG']
Notes:
file_glob corresponds to the rules of *.jpg under the C:\Users\Administrator\Desktop\images\training\ path;
glob.glob(file_glob) returns all jpg image path names that meet the rules;