Will Downs
Will Downs

Reputation: 101

Image.open PermissionError: [Errno 13] Permission denied:

I am making an image classifier to classify rockets and airplanes using Python and TensorFlow, but I'm having trouble loading my training images folder with Pil.Image.Open. This is my code:

train_data = "C:/Users/Will Downs/image_training/training_data/"
test_data = "C:/Users/Will Downs/image_training/test_data/"

def train_data_with_label():
  train_images = []
  for i in tqdm(os.listdir(train_data)):
    path = os.path.join(train_data, i)
    img = Image.open(path)
    img.thumbnail((64, 64), Image.ANTIALIAS)  # resizes image in-place
    train_images.append([np.array(img), one_hot_label(i)])
  shuffle(train_images)
  return train_images

def test_data_with_label():
  test_images = []
  for i in tqdm(os.listdir(test_data)):
    path = os.path.join(test_data, i)
    img = Image.open(path)
    img.thumbnail((64, 64), Image.ANTIALIAS)  # resizes image in-place
    test_images.append([np.array(img), one_hot_label(i)])
  shuffle(test_images)
  return test_images

This is the error I get:

PermissionError                           Traceback (most recent call last)
<ipython-input-17-f3b44f76f884> in <module>
     46   return test_images
     47 
---> 48 training_images = train_data_with_label()
     49 testing_images = test_data_with_label()
     50 tr_img_data = np.array([i[0] for i in training_images]).reshape(-1,64,64,1)

<ipython-input-17-f3b44f76f884> in train_data_with_label()
     30   for i in tqdm(os.listdir(train_data)):
     31     path = os.path.join(train_data, i)
---> 32     img = Image.open(path)
     33     img.thumbnail((64, 64), Image.ANTIALIAS)  # resizes image in-place
     34     train_images.append([np.array(img), one_hot_label(i)])

~\Anaconda3\lib\site-packages\PIL\Image.py in open(fp, mode)
   2768 
   2769     if filename:
-> 2770         fp = builtins.open(filename, "rb")
   2771         exclusive_fp = True
   2772 

PermissionError: [Errno 13] Permission denied: 'C:/Users/Will Downs/image_training/training_data/Airplane'

Any suggestions on why this is or how I can fix it?

Upvotes: 0

Views: 1665

Answers (1)

Will Downs
Will Downs

Reputation: 101

The issue was a simple folder formatting one. I had the images in folders based on their label, instead of being pooled together but named according to their label.

Upvotes: 0

Related Questions