如何在使用matplotlib时将图像保存到文件中

2024-10-02 22:34:53 发布

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

我正在使用不同的过滤器从skimage包&希望保存新的图像到文件,而不是显示在屏幕上。你能帮忙吗。我是python的新用户。下面是我的代码

import os
a = 'C:'
a1 = 'Users'
a2 = 'ashay.chandra'
b = 'Downloads' 
c = 'test.jpg'
import matplotlib.pyplot as plt

from skimage import data,io
from skimage.color import rgb2hed
from matplotlib.colors import LinearSegmentedColormap

# Create an artificial color close to the orginal one
cmap_hema = LinearSegmentedColormap.from_list('mycmap', ['white', 'navy'])
cmap_dab = LinearSegmentedColormap.from_list('mycmap', ['white',
                                             'saddlebrown'])
cmap_eosin = LinearSegmentedColormap.from_list('mycmap', ['darkviolet',
                                               'white'])
filename = os.path.join (a + os.sep,a1,a2,b,c)
#ihc_rgb = data.immunohistochemistry()
img = io.imread(filename)
#ihc_rgb = data.immunohistochemistry()
ihc_hed = rgb2hed(img)

fig, axes = plt.subplots(2, 2, figsize=(7, 6), sharex=True, sharey=True,
                         subplot_kw={'adjustable': 'box-forced'})
ax = axes.ravel()
plt.savefig("'C:\Users\ashay.chandra\Documents\learning_python\test_Orig.png") = ax[0].imshow(img)
plt.savefig("'C:\Users\ashay.chandra\Documents\learning_python\4PnormC1P2_AD1_TZ1_A0_V0_M0_L0_IX_H1_Hematoxylin.png") = ax[1].imshow(ihc_hed[:, :, 0], cmap=cmap_hema)
plt.savefig("'C:\Users\ashay.chandra\Documents\learning_python\test_Eosin.png") = ax[2].imshow(ihc_hed[:, :, 1], cmap=cmap_eosin)
plt.savefig("'C:\Users\ashay.chandra\Documents\learning_python\test_DAB.png") = ax[3].imshow(ihc_hed[:, :, 2], cmap=cmap_dab)
#ax[0].imshow(img)
#ax[0].set_title("Original image")

#ax[1].imshow(ihc_hed[:, :, 0], cmap=cmap_hema)
#ax[1].set_title("Hematoxylin")

#ax[2].imshow(ihc_hed[:, :, 1], cmap=cmap_eosin)
#ax[2].set_title("Eosin")

#ax[3].imshow(ihc_hed[:, :, 2], cmap=cmap_dab)
#ax[3].set_title("DAB")

for a in ax.ravel():
    a.axis('off')

fig.tight_layout()

有人能帮忙吗

阿沙伊


Tags: fromtestimportimgpltaxuserscmap