Search code examples
pythonpngjpeg

The code below only sees photos with jpg extension. How should I change the syntax to include photos in png,jpg,jpeg format


The code below only sees photos with jpg extension. How should I change the syntax to include photos in png,jpg,jpeg format

image_df_train_f = pd.DataFrame({'path': list(Path(input_path).glob('**/*.jp*g'))})

Solution

  • To list files with different extensions, you can use glob like this:

    EXTENSIONS = (".png", ".jpg", ".jpeg")
    path = [p.resolve()
            for p in Path(input_path).glob("**/*")
            if p.suffix in EXTENSIONS]
    image_df_train_f = pd.DataFrame({'path': path})
    

    Be aware that the solution you provided in the comments, while concise, may list unnecessary files, so you should not use it.

    image_df_train_f = pd.DataFrame({'path': list(Path(input_path).glob("*.[jp][pn]g"))})
    

    While it does list files with .jpg and .png (without .jpeg, by the way) extensions, it also includes .jng and .ppg files it locates in the directory.