0

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'>
DeeWBee
  • 695
  • 4
  • 13
  • 38
  • 1
    Maybe add the full stacktrace? Does this error happen at `im_coll = io.ImageCollection(imagePath)` line or where? – Dilettant Jul 07 '16 at 17:00
  • @Dilettant the error will occur at the `image = im_coll[i]` line. When I play with `im_coll` in the console, I can successfully execute `im_coll[i]` for any files that don't have an underscore, but receive a similar error for any files that do contain an underscore. I'm editing my question to include the stack trace. – DeeWBee Jul 07 '16 at 17:01
  • 2
    It may be unrelated (or just hidden in the skimage "sphere"), but could help to skim through [Image.open() cannot identify image file - Python?](http://stackoverflow.com/questions/19230991/image-open-cannot-identify-image-file-python) – Dilettant Jul 07 '16 at 17:08
  • @Dilettant ah I already looked at that thread, didn't yield any results. But thanks! – DeeWBee Jul 07 '16 at 18:46

1 Answers1

1

OKAY! So. It appears that I've been working with corrupted image files the entire time, hence the errors.

DeeWBee
  • 695
  • 4
  • 13
  • 38