Matplotlib pyplot保存张量中的多个图像



我在批处理张量(B、H、W、C(中有图像,我想用不同的名称将它们逐个保存在文件夹中。

for t in range(0,batch_size):
a = target_silhouette[t].cpu().numpy()
plt.savefig("./test.png")
plt.imshow(a)

有什么建议吗?

如果您的代码有效,请这样更改:

import os
d,n = os.path.split(__file__)
for t in range(0,batch_size):
a = target_silhouette[t].cpu().numpy()
plt.savefig(d + "\test" + str(t) + ".png")
plt.imshow(a)

我能够创建一个函数

def plot_image(image):
image_save = image.cpu().numpy()    
fig = plt.figure(figsize=(5, 5))
plt.grid("off");
plt.axis("off");
plt.imshow(image_save)

然后使用这个循环

plot_period = 1
for t in range(0,3):
new_image = target_silhouette[t]
if t % plot_period == 0:
plot_image(new_image)
plt.savefig( "test" + str(t) + ".png")

最新更新