如何将张量写入PNG图像文件?

问题描述 投票:0回答:1

我正在尝试将具有以下属性的张量写入PNG文件。

type(adv_x) 
<class 'tensorflow.python.framework.ops.EagerTensor'>

adv_x.shape
(1, 600, 800, 3)

tf.rank(adv_x) 
tf.Tensor(4, shape=(), dtype=int32)

我知道如何将张量显示为图像(plt.imshow(adv_x[0]),但我想将其写入文件,以便最终得到600 x 800像素的RGB .PNG文件。

提前感谢。

machine-learning tensorflow image
1个回答
0
投票

您可以从python使用Pillow库:

from PIL import Image

image = Image.fromarray(adv_x[0])
image.save('name.png', format='PNG')

[库中还有许多其他功能,可用于以各种方式处理图像。进一步查看blog

© www.soinside.com 2019 - 2024. All rights reserved.