2016-04-23 99 views
0
from PIL import Image 
image1 = "Image_I0000_F1_Filter 1_1A_health_2014-05-20_11.05.33.483.tiff" 
image2 = "*F1*.tiff" 
im1 = Image.open(image1) 
im2 = Image.open(image2) 

試圖打開相同的圖像。 im1打開沒有問題,但im2顯示IOError:[Errno 2]沒有這樣的文件或目錄:'* F1 * .tiff'。Python Image.open()無法識別正則表達式

也試過

image2 = r"*F1*.tiff" 
im2 = Image.open(image2) 

image2 = "*F1*.tiff" 
im2 = Image.open(open(image2,'rb')) 

既不作品。

+0

它應該是? –

+0

你確定它存在(圖片和程序試圖訪問它的位置) – Li357

+1

這看起來像一個錯誤報告,我將在「沒有錯誤」下提交。 –

回答

1

PIL.Image.open沒有全局匹配項。 The documentation建議

You can use either a string (representing the filename) or a file object as the file argument

值得注意的是不包括全局匹配。

Python使用glob模塊進行全局匹配。

from PIL import Image 

import glob 

filenames = glob.glob("*F1*.tiff") 
# gives a list of matches, in this case most likely 
# # ["Image_I0000_F1_Filter 1_1A_health_2014-05-20_11.05.33.483.tiff"] 
if filenames: 
    filename = filenames[0] 
else: 
    # what do we do if there's no such file? I guess pass the empty string 
    # to Image and let it deal with it 
    filename = "" 
    # or maybe directly... 
    raise FileNotFoundError 

im1 = Image.open(filename)