Chr*_*ant 2 image machine-learning tensorflow
我正在尝试将具有以下属性的张量写入 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)
Run Code Online (Sandbox Code Playgroud)
我知道如何将张量显示为图像 ( plt.imshow(adv_x[0])),但我想将其写入文件,以便最终得到 600 x 800 像素的 RGB .PNG 文件。
提前致谢。
小智 5
留在 TF 领域通常更可取。因此,在 TF 2.0 中,您也可以简单地使用
tf.keras.preprocessing.image.save_img('path/to/file/file.png',adv_x[0])
它在后台使用 PIL 并为您提供所有灵活性,但不需要额外导入或转换张量。
您可以使用python 中的Pillow库:
from PIL import Image
image = Image.fromarray(adv_x[0])
image.save('name.png', format='PNG')
Run Code Online (Sandbox Code Playgroud)
库中还有许多其他函数可用于以各种方式操作图像。查看此博客了解更多信息。
| 归档时间: |
|
| 查看次数: |
3774 次 |
| 最近记录: |