【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())

你可能感兴趣的:(python#,tensorflow)