Python深度学习003:读取单张图片

    科技2025-08-28  11

    # -*- coding: utf-8 -*- import tensorflow as tf import matplotlib.pyplot as plt def read_image(file_name): img = tf.read_file(file_name) # 默认读取格式为uint8 print("type(img):", type(img)) img = tf.image.decode_jpeg(img, channels=0) # channels 为1得到的是灰度图,为0则按照图片格式来读 return img def main(): with tf.device("/cpu:0"): img_path = "R1.JPG" img = read_image(img_path) with tf.Session() as sess: image_numpy = sess.run(img) print(image_numpy) print(image_numpy.dtype) print(image_numpy.shape) plt.imshow(image_numpy) plt.show() if __name__ == '__main__': main()
    Processed: 0.012, SQL: 8