将f, axarr = plt.subplots(1,4) axarr[0].title.set_text('Original \n Image') axarr[1].title.set_text('Reconstructed Image with \n 43% Compression') axarr[2].title.set_text('Reconstructed Image with \n 68% Compression') axarr[3].title.set_text('Reconstructed Image with \n 84% Compression') for i in range(4): axarr[i].title.set_fontsize(15) axarr[0].imshow((valid_batch[0].cpu().detach().permute(1, 2, 0) * 0.5) + 0.5) axarr[1].imshow((reconstructed_img_28[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5) axarr[2].imshow((reconstructed_img_16[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5) axarr[3].imshow((reconstructed_img_8[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5) f.set_figheight(5) f.set_figwidth(20) plt.show()中显示的图像分别保存为.png文件
时间: 2023-06-26 16:09:03 浏览: 70
matplotilb中fig,ax = plt.subplots()的使用
可以在每个imshow()函数后添加savefig()函数来保存图像,如下所示:
```
f, axarr = plt.subplots(1,4)
axarr[0].title.set_text('Original \n Image')
axarr[1].title.set_text('Reconstructed Image with \n 43% Compression')
axarr[2].title.set_text('Reconstructed Image with \n 68% Compression')
axarr[3].title.set_text('Reconstructed Image with \n 84% Compression')
for i in range(4):
axarr[i].title.set_fontsize(15)
axarr[0].imshow((valid_batch[0].cpu().detach().permute(1, 2, 0) * 0.5) + 0.5)
axarr[1].imshow((reconstructed_img_28[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5)
axarr[2].imshow((reconstructed_img_16[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5)
axarr[3].imshow((reconstructed_img_8[0].cpu().detach().permute(1, 2, 0) *0.5) + 0.5)
# 保存图像
plt.savefig(f"reconstructed_img_{i}.png")
f.set_figheight(5)
f.set_figwidth(20)
plt.show()
```
这将在脚本所在的目录中保存四个图像,分别命名为`reconstructed_img_0.png`、`reconstructed_img_1.png`、`reconstructed_img_2.png`和`reconstructed_img_3.png`。
阅读全文