得到shape
input_shape = [4,224,224,4]
input = tf.random_normal(input_shape) #转成tensor
inputs_shape = input.get_shape().as_list() #转成list
print(inputs_shape)
[4,224,224,4]
tensor转ndarray
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
image_path = r"E:\data\178955836273545-Back.jpg"
#image_string = open(image_path,'rb').read()
#image_string = tf.gfile.FastGFile(image_path, 'rb').read() # 字节
image_string = tf.io.read_file(image_path)
image = tf.image.decode_jpeg(image_string) # 2.图片解码
with tf.Session() as sess:
print(sess.run(image).shape)
image = image.eval() # 将tensor对象转成数组
print(image.shape)
plt.imshow(image)
plt.show()
image = tf.expand_dims(image, 0)
#image = np.asarray(sess.run(image), dtype='uint8') # TF处理过的图片自动转换了类型,需要调整回uint8才能正常显示,将tensor转为ndarray
image = tf.image.convert_image_dtype(image,dtype=tf.uint8)
image = image.eval()
height, width, channels = image.shape[1:]
image = image.reshape(height, width, channels)
plt.imshow(image)
plt.show()