Home > Enterprise >  How to load all the image paths from a directory into a pandas dataframe column?
How to load all the image paths from a directory into a pandas dataframe column?

Time:05-02

I have a directory of images, and I want to load all the paths to each image into a pandas column. However I am not sure how I can extract every path and put them into the column. Is there a built in function that will iterate over all of the images in the directory and put the paths from each one into a pandas column? Or would I use some sort of for loop? To specify, I need the actual paths, not the image names, as I am wanting to use the flow_from_dataframe tensorflow function as I have labels in a pandas dataframe. I am still quite new to this so help would be appreciated.

CodePudding user response:

Lets call the directory that contains the images source_dir. Then try

import os
import pandas as pd
filepaths=[]
filelist=os.listdir(source_dir)
for f in filelist:
    fpath=os.path.join(source_dir,f)
    filepaths.append(fpath)
Fseries=pd.Series(filepaths, name='filepaths')
dataframe=pd.concat([Fseries], axis=1)
print (df.head())
  • Related