TensorFlow该如何保存图片

TensorFlow该如何保存图片,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

# 根据路径读取图片
img = tf.io.read_file(img_path)
# 解码图片,这里应该是解码成了png格式
img = tf.image.decode_png(img, channels=1)
# 大小缩放
img = tf.image.resize(img, [28, 28])
# 这一步转换张量数据类型很重要
img = tf.cast(img, dtype=tf.uint8)
# 编码回图片
img = tf.image.encode_png(img)
# 保存
with tf.io.gfile.GFile(img_path, 'wb') as file:
    file.write(img.numpy())

看完上述内容,你们掌握TensorFlow该如何保存图片的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!