簡體   English   中英

Python 3.5:打開文件名帶有下划線的圖像嗎?

[英]Python 3.5: Open an image with underscore in file name?

我有一個腳本(如下所示)可以打開圖像集合。 我花了一些時間才弄清楚這一點,但我意識到該腳本似乎無法打開名稱中帶有下划線的任何圖像文件。 我的圖像目錄的示例如下:

Images\\img1.jpg

Images\\img2.jpg

Images\\img3_c.jpg

當處理“普通”圖像文件名時,腳本將表現出預期的效果。 腳本必須打開名稱中帶有下划線的文件時,會返回如下錯誤:

OSError: cannot identify image file <_io.BufferedReader name='TrainingSets\\CottonWoolSpots\\0006_c.jpg'>

如果可能,我想避免更改文件名。 任何幫助表示贊賞。

腳本:

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()

編輯完整堆棧+錯誤:

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'>

好的! 所以。 看來我一直都在使用損壞的圖像文件,因此出現了錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM