我正在尝试将具有以下属性的张量写入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文件。
提前感谢。
回答:
您可以使用Python的Pillow库:
from PIL import Imageimage = Image.fromarray(adv_x[0])image.save('name.png', format='PNG')
该库中有许多其他功能,可以以各种方式操作图像。请查看这篇博客了解更多信息。