1. tf.image.decode_jpeg(contents, channels=None, ratio=None, fancy_upscaling=None, try_recover_truncated=None, acceptable_fraction=None, name=None)
Decode a JPEG-encoded image to a uint8 tensor. 将一张jpeg的图片解码成uint8的张量,形状3-D, [height, width, channels]
contents 是编码过的图片,是一个类型为string的tensor,形状0-D,
channels 代表通道,如果为默认的0,则表示使用编码图片的通道数,若为1,则输出为灰度图,若为3,则输出为rgb格式
eg:
import tensorflow as tf
import numpy as np
img_path = '/Users/apple/Downloads/Medlinker/util/1.jpg' # 图片存放的路径
def decode_img(path):
file_queue = tf.train.string_input_producer([path]) # 注意这儿的输入是一个列表,参数还有shuffle,是否打乱
image_reader = tf.WholeFileReader()
key, image = image_reader.read(file_queue)
image_decode = tf.image.decode_jpeg(image, channels=0)
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)
# sess.run(tf.global_variables_initializer())
x = sess.run(image_decode)
coord.reques