Home > database >  Trying to plot images for specific file names
Trying to plot images for specific file names

Time:02-19

I am trying to print a bunch of pictures from my folder using matplotlib however I am not sure how to fixe this error.

Basically, I am trying to get specific files with the pattern ' _ 0 _ ' or ' _ 1 _ ' but It's giving me an error and not sure how to fixe it.

I put a screenshot of the example of code I am trying to replicate, let me know if you have questions. Any help is welcome!

import os
import matplotlib.pyplot as plt
from matplotlib.image import imread
#folder = '/content/drive/MyDrive/Colab Notebooks/data/dogs-vs-cats/train/'
plt.figure(figsize=(10,10))
for i in range(10): 
    plt.subplot(4,5,i 1)
    filename = glob.glob(os.path.join(folder,'*_1_*.jpg'))
    #for filename in folder: 
    #filename = glob.glob(folder  '*_1_*.jpg')
    #filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
    #filename = folder   '*_1_*.jpg'
    if not os.path.exists(filename):
    #if not os.path.exists(filename):
      print ('No such file:'  filename)
    image = imread(filename)
    plt.imshow(image)
for i in range(10): 
    plt.subplot(4,5,i 11)
    plt.subplot(4,5,i 11)
    plt.subplot(4,5,i 11)
    filename = glob.glob(os.path.join(folder,'*_0_*.jpg'))
    #filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_0_*.jpg')
    #filename = folder   ' *_0_*.jpg'
    image = imread(filename)
    plt.imshow(image)
plt.show()

ERROR MESSAGE:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-8c96486bb2b7> in <module>()
     11     #filename = glob.glob('/content/drive/MyDrive/Colab Notebooks/train_val'/'*_1_*.jpg')
     12     #filename = folder   '*_1_*.jpg'
---> 13     if not os.path.exists(filename):
     14     #if not os.path.exists(filename):
     15       print ('No such file:'  filename)

/usr/lib/python3.7/genericpath.py in exists(path)
     17     """Test whether a path exists.  Returns False for broken symbolic links"""
     18     try:
---> 19         os.stat(path)
     20     except OSError:
     21         return False

TypeError: stat: path should be string, bytes, os.PathLike or integer, not list

What I am trying to accomplish : enter image description here

CodePudding user response:

The glob.glob() function returns a list, and the error message is telling you that

TypeError: stat: path should be string, bytes, os.PathLike or integer, not list

Your loop should be something like this, instead:


for i in range(10): 
    plt.subplot(4, 5, i 1)
    filelist = glob.glob(os.path.join(folder, '*_1_*.jpg'))
    to_process = []
    for entry in filelist:
        if not os.path.exists(entry):
            print ("File {entry} does not exist".format(entry=entry))
            continue
        else:
            to_process.append(entry)

    for entry in to_process:
        # ... other desired processing

  • Related