2016-04-23 93 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)