吴裕雄 python 神经网络——TensorFlow图片预处理

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# 使用'r'会出错,无法解码,只能以2进制形式读取
# img_raw = tf.gfile.FastGFile('E:\\myresource\\moutance.jpg','rb').read()
img_raw = open('E:\\myresource\\moutance.jpg','rb').read()

# 把二进制文件解码为uint8
img_0 = tf.image.decode_png(img_raw)
# 可以用np直接转换了
# img_1 = tf.image.convert_image_dtype(img_0,dtype=tf.uint8)

sess = tf.Session()
print(sess.run(img_0).shape)
plt.imshow(sess.run(img_0))
plt.show()

def show_pho(img,sess=sess):
    '''
    TF处理过的图片自动转换了类型,需要调整回uint8才能正常显示
    :param sess: 
    :param img: 
    :return: 
    '''
    moutance = np.asarray(sess.run(img),dtype='uint8')
    print(moutance.shape)
    plt.imshow(moutance)
    plt.show()

'''调整图像大小'''
# 插值尽量保存原图信息
img_1 = tf.image.resize_images(img_0,[500,500],method=3)
show_pho(img_1)

# 裁剪或填充
# 自动中央截取
img_2 = tf.image.resize_image_with_crop_or_pad(img_0,500,500)
show_pho(img_2)

# 比例中央裁剪
img_4 = tf.image.central_crop(img_0,0.5)
show_pho(img_4)

 

转载于:https://www.cnblogs.com/tszr/p/10822050.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值