'IsADirectoryError: [Errno 21] Is a directory: '/'
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
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |