import tensorflow as tf
import numpy as np
from matplotlib import pyplot as plt
img_path = r'C:\Users\Desktop\py\TF2_0\Dogs\test\chihuahua\01.jpg'
# 读入图片
img = tf.io.read_file(img_path)
# 解码为tensor格式
img = tf.image.decode_jpeg(img)
print('shape:', img.shape, 'dtype:', img.dtype)
# 将图片转换问灰度图片,即最后一维只有1
img = tf.image.rgb_to_grayscale(img)
# 剪裁图片, 高度和宽度
img = tf.image.resize_with_crop_or_pad(img, 250, 151)
# 编码tensor
img = tf.image.encode_jpeg(img, quality= 100)
# 写入./02.jpg这个文件里面
with tf.io.gfile.GFile('./02.jpg', 'wb') as file:
file.write(img.numpy())
03-27
1753
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
06-24
3669
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-18
659
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-23
948
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交