当我试图使用matplotlib将图像保存为tif文件格式时,为什么会出现“ValueError:未知文件扩展名”?

2024-04-20 14:51:07 发布

您现在位置:Python中文网/ 问答频道 /正文

在这段代码中,我尝试使用一个预加载的机器学习模型和一个预定义的特征提取函数对我的Train_images目录中的多个图像执行图像分割。我的代码一直运行到“print(file)”一行,在这一点上,我不确定会发生什么。这是我用来处理tif图像文件训练集的代码

import glob
import pickle
from matplotlib import pyplot as plt

filename = "sandstone_rf_model"
loaded_model = pickle.load(open(filename, 'rb'))

path = "images/Train_images/*.tif"
for file in glob.glob(path):
print(file)  # just stop here to see all file names 
printed
img1 = cv2.imread(file)
img = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)

# Call the feature extraction function.
X = feature_extraction(img)
result = loaded_model.predict(X)
segmented = result.reshape((img.shape))

name = file.split("e_")
plt.imsave('images/Segmented/', segmented, cmap='jet')

编辑: 之前我得到了一个ValueError,但我将最后一行代码更改为:

plt.imsave('images/segmented_sandstone/'+name[1], segmented, cmap='jet')

现在我收到以下KeyError:

`  File 
   "/Users/zeeshanpatel/opt/anaconda3/envs/master/lib/python3.7/site- 
   packages/PIL/Image.py", line 2123, in save
   save_handler = SAVE[format.upper()]
   KeyError: 'TIF'`

问题发生在代码的最后一行,请告诉我如何格式化此文件以解决此问题


Tags: 代码图像importimgmodeltrainpltcv2