Image processing

In [1]:
import tensorflow as tf
from PIL import Image
In [2]:
filename = 'pics/cat.881.jpg'
image = Image.open(filename)
print(image)
image
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x395 at 0x7FE3600BF470>
Out[2]:

tf.convert_to_tensor

Works on both numpy and PIL objects.

Returns a uint8 tensor

In [3]:
image_tensor = tf.convert_to_tensor(image)
print(image_tensor)
Tensor("Const:0", shape=(395, 500, 3), dtype=uint8)

Resizing

tf.image.resize_images expects a float tensor (with pixels in range 0-1) and returns a float_tensor

Use tf.image.convert_image_dtype. Using tf.cast will destroy the image.

When converting to float image, make sure the input is uint8. If it is int32/64, the resulting values are close to zero

In [17]:
float_image_tensor = tf.image.convert_image_dtype(image_tensor,tf.float32)
print(float_image_tensor)
float_resized_image = tf.image.resize_images(float_image_tensor,(224,224))
print(resized_image)
Tensor("convert_image_1:0", shape=(395, 500, 3), dtype=float32)
Tensor("resize_images_2/Squeeze:0", shape=(224, 224, 3), dtype=float32)

PIL.Image.fromarray expects a uint array

In [27]:
uint_resized_image = tf.image.convert_image_dtype(float_resized_image,tf.uint8)
print(unint_resized_image)
with tf.Session() as sess:
    out = sess.run(uint_resized_image)
    print(out.shape)

Image.fromarray(out)
Tensor("convert_image_2:0", shape=(224, 224, 3), dtype=uint8)
(224, 224, 3)
Out[27]:

Central crop

Works on both uint and float tensors

In [38]:
central_crop_image = tf.image.central_crop(image_tensor,central_fraction=0.5)
#central_crop_image = tf.image.central_crop(float_image_tensor,central_fraction=0.5)
central_crop_image
Out[38]:
<tf.Tensor 'central_crop_5/Slice:0' shape=(199, 250, 3) dtype=uint8>
In [39]:
with tf.Session() as sess:
    out = sess.run(tf.image.convert_image_dtype(central_crop_image,tf.uint8))
    print(out.shape)

Image.fromarray(out)
(199, 250, 3)
Out[39]:

Flip up and down

Works on both uint and float tensors

In [48]:
flip_image = tf.image.flip_up_down(float_resized_image)
#flip_image = tf.image.flip_up_down(uint_resized_image)

print(flip_image)
Tensor("flip_up_down_5/ReverseV2:0", shape=(224, 224, 3), dtype=float32)
In [49]:
with tf.Session() as sess:
    out = sess.run(tf.image.convert_image_dtype(flip_image,tf.uint8))
    print(out.shape)

Image.fromarray(out)
(224, 224, 3)
Out[49]:

Color adjustments

For all color adjustment operations like brightness, contrast, saturation, hue, tensorflow expects uint image. Does NOT work on float images

In [59]:
bright_image = tf.image.adjust_brightness(uint_resized_image,delta=0.5)
#bright_image = tf.image.adjust_brightness(float_resized_image,delta=0.5) #Doesnt work
print(bright_image)
Tensor("adjust_brightness_4/convert_image_1:0", shape=(224, 224, 3), dtype=uint8)
In [60]:
with tf.Session() as sess:
    out = sess.run(tf.image.convert_image_dtype(bright_image,tf.uint8))
    print(out.shape)

Image.fromarray(out)
(224, 224, 3)
Out[60]:

Random adjustments

Use tf.image.random_ methods

See TF-Exercise for examples

Converting RGB to gray scale

In [5]:
grey_image = tf.image.rgb_to_grayscale(image_tensor)
grey_image
Out[5]:
<tf.Tensor 'rgb_to_grayscale_1:0' shape=(395, 500, 1) dtype=uint8>
In [7]:
with tf.Session() as sess:
    out = sess.run(grey_image)
    print(out.shape)

Image.fromarray(out.reshape(395,500))
(395, 500, 1)
Out[7]: