0

I am trying to use Images stored inside the directory by using this code,

import random

path = "/content/drive/MyDrive/Colab Notebooks/Low Images/*.png"
low_light_images = glob(path)
for image_path in random.choice(low_light_images):
    original_image, output_image = inferer.infer(image_path)
    plot_result(original_image, output_image)

But getting this error,

---------------------------------------------------------------------------
IsADirectoryError                         Traceback (most recent call last)
<ipython-input-62-668719a88426> in <module>()
      4 low_light_images = glob(path)
      5 for image_path in random.choice(low_light_images):
----> 6     original_image, output_image = inferer.infer(image_path)
      7     plot_result(original_image, output_image)

1 frames
/usr/local/lib/python3.7/dist-packages/PIL/Image.py in open(fp, mode)
   2841 
   2842     if filename:
-> 2843         fp = builtins.open(filename, "rb")
   2844         exclusive_fp = True
   2845 

IsADirectoryError: [Errno 21] Is a directory: '/'

How can I resolve this? Full Code Link: here

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
micro
  • 43
  • 1
  • 7
  • Can you show the code that is generating this issue. What you've posted is not runnable – DarkKnight Feb 05 '22 at 19:31
  • Here is the code [link](https://drive.google.com/file/d/1E9nsSvY8kWmloI6nb2q80uFAQaGOWNMk/view?usp=sharing), and Google Colab [link](https://drive.google.com/file/d/1hvYTJF5P8__OCq8U6e04kXB3NqNRDVal/view?usp=sharing) hope this time it will open. – micro Feb 06 '22 at 05:14

1 Answers1

0

The line

for image_path in random.choice(low_light_images):

is grabbing a random filepath such as

for image_path in "/content/drive/MyDrive/Colab Notebooks/Low Images/some_image.png":

And when the for loop starts image_path will end up containing the first character which is a /, and you can see the problem.

To randomly loop over all the data use random.shuffle. (there is alsorandom.choices with an s at the end and random.sample that will grab a random subset of all your images).

low_light_images = glob(path)
random.shuffle(low_light_images)
for image_path in low_light_images:

It's easiest to debug stuff if you can simplify the problem to a short MVCE. When using random functions, I will replace the random function with an example of something that it outputs that creates the error condition, like I showed above. Another thing I do is sometimes I will need a sanity check that my variables contain the data I think they contain, and so I will print them out (or you can use a debugger). Doing that we would see that image_path contains a / instead of the expected file path.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • Thanks for your answer `random.choices` helped. But if I want to access all images sequentially, not randomly, what will be the best way? – micro Feb 06 '22 at 05:37
  • For that you can use the [sorted](https://docs.python.org/3/library/functions.html#sorted) function. Sorted has a parameter called key that lets you sort the function however you like. To ignore if something is upper/lowercase use `key=str.casefold`. If you have a mix of numbers and letters see [how to natural sort](https://stackoverflow.com/questions/4836710/is-there-a-built-in-function-for-string-natural-sort) – hostingutilities.com Feb 06 '22 at 19:18