Tensorflow image reading & display

Tensorflow image reading & display

Asked on December 26, 2018 in Tensorflow.
Add Comment


  • 3 Answer(s)

    Let’s try this code for image reading & display :

    filename_queue = tf.train.string_input_producer(['/Users/HANEL/Desktop/tf.png']) #  list of files to read
     
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
     
    my_img = tf.image.decode_png(value) # use png or jpg decoder based on your files.
     
    init_op = tf.global_variables_initializer()
    with tf.Session() as sess:
      sess.run(init_op)
     
      # Start populating the filename queue.
     
      coord = tf.train.Coordinator()
      threads = tf.train.start_queue_runners(coord=coord)
     
      for i in range(1): #length of your filename list
        image = my_img.eval() #here is your image Tensor
     
      print(image.shape)
      Image.fromarray(np.asarray(image)).show()
     
      coord.request_stop()
      coord.join(threads)
    
    Answered on December 26, 2018.
    Add Comment

    To decode JPEG/PNG images Click here.

    Example:

    import tensorflow as tf
     
    filenames = ['/image_dir/img.jpg']
    filename_queue = tf.train.string_input_producer(filenames)
     
    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
     
    images = tf.image.decode_jpeg(value, channels=3)
    
    Answered on December 26, 2018.
    Add Comment

        Using tf.train.match_filenames_once load names, with tf.size get the number of files to iterate.

    import tensorflow as tf
    import numpy as np
    import matplotlib;
    from PIL import Image
     
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
     
     
    filenames = tf.train.match_filenames_once('./images/*.jpg')
    count_num_files = tf.size(filenames)
    filename_queue = tf.train.string_input_producer(filenames)
     
    reader=tf.WholeFileReader()
    key,value=reader.read(filename_queue)
    img = tf.image.decode_jpeg(value)
     
    init = tf.global_variables_initializer()
    with tf.Session() as sess:
        sess.run(init)
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(coord=coord)
        num_files = sess.run(count_num_files)
        for i in range(num_files):
            image=img.eval()
            print(image.shape)
            Image.fromarray(np.asarray(image)).save('te.jpeg')
    
    Answered on December 26, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.