TensorFlow | Image processing with tf.io and tf.image

TensorFlow provides tf.io and tf.image modules for reading and processing the images. Below is the code snippet for reading and processing images with tf.io and tf.image . RUn the code snippet in Jupyter or Colab notebook.

  • Download the sample image by clicking here and keep it in working directory with name flower.jpg
  • Import modules and read image from file
  • 
    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    image=tf.io.read_file("flower.jpg")
    image = tf.io.decode_jpeg(image)
    
    
  • Print original image
  • 
    plt.figure()
    plt.imshow(image)
    
    flower
  • Flipping image left to right with tf.image
  • 
    image = tf.image.flip_left_right(image)
    plt.figure()
    plt.imshow(image)
    
    
    flower
  • Flipping image upside down with tf.image
  • 
    image = tf.image.flip_up_down(image)
    plt.figure()
    plt.imshow(image)
    
    flower
  • Adjusting brightness of image with tf.image
  • 
    image = tf.image.adjust_brightness(image, delta=0.5)
    plt.figure()
    plt.imshow(image)
    
    flower

    Categories: TensorFlow