I have a script (seen below) that opens a collection of images. It took me a while to figure this out, but I realized that the script can't seem to open any of the image files with an underscore in the name. An example of my image directory is as follows:
Images\img1.jpg
Images\img2.jpg
Images\img3_c.jpg
The script will behave as expected when dealing with "normal" image file names. The moment the script has to open a file with an underscore in the name, it returns an error as follows:
OSError: cannot identify image file <_io.BufferedReader name='TrainingSets\\CottonWoolSpots\\0006_c.jpg'>
I would like to avoid changing my file names if possible. Any help is appreciated.
Script:
import numpy as np
from skimage import io
import glob
import os
def importAllImgs():
imagePath = glob.glob("TrainingSets\CottonWoolSpots\*.jpg")
im_coll = io.ImageCollection(imagePath)
im_array = []
for i in range(len(im_coll)):
image = im_coll[i]
im_array.append(image)
return im_array
if __name__ == "__main__":
testArray = importAllImgs()
EDIT FULL STACKTRACE + ERROR:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 734, in debugfile
debugger.run("runfile(%r, args=%r, wdir=%r)" % (filename, args, wdir))
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\bdb.py", line 431, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
execfile(filename, namespace)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "//mspm1bnas50s/home58/biegad1/python scripts/batchimportimgs_0_1.py", line 54, in <module>
testArray = importAllImgs()
File "//mspm1bnas50s/home58/biegad1/python scripts/batchimportimgs_0_1.py", line 36, in importAllImgs
image = im_coll[i]
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\collection.py", line 264, in __getitem__
self.data[idx] = self.load_func(self.files[n], **kwargs)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\_io.py", line 61, in imread
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\manage_plugins.py", line 211, in call_plugin
return func(*args, **kwargs)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\skimage\io\_plugins\pil_plugin.py", line 36, in imread
im = Image.open(f)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\site-packages\PIL\Image.py", line 2309, in open
% (filename if filename else fp))
OSError: cannot identify image file <_io.BufferedReader name='TrainingSets\\CottonWoolSpots\\0006_c.jpg'>