欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TensorFlow批读取图片

程序员文章站 2022-03-20 17:22:59
...
# coding:utf-8

from __future__ import print_function
import os
import tensorflow as tf


def TF_image_data(path_to_images):  
    """
    using tensorflow to read and store data
    """
    if not tf.gfile.Exists(path_to_images):
        print("path to images does not exist")
    else:
        #search all jpeg files
        path_img_list = files = os.listdir(path_to_images)
        #path_img_list = tf.gfile.Glob(os.path.join(path_to_images, '*.jpg'))
        print(path_img_list)
        total_img = []
        for file in path_img_list:
            print("the name of jpeg file:", file)
            img_jpg = tf.gfile.FastGFile(path_to_images +'/' +file,'rb').read() 
            img_decode = tf.image.decode_jpeg(img_jpg)
            img_data = tf.image.convert_image_dtype(img_decode, dtype = tf.float32)
            total_img.append(img_data)
        print(len(total_img))
        return total_img 


if __name__ == '__main__':
    path_to_images = './Images'
    with tf.Session() as sess:
        TF_image_data(path_to_images)