import tensorflow as tf
from PIL import Image
filename = 'pics/cat.881.jpg'
image = Image.open(filename)
print(image)
image
tf.convert_to_tensor
Works on both numpy and PIL objects.
Returns a uint8 tensor
image_tensor = tf.convert_to_tensor(image)
print(image_tensor)
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
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)
PIL.Image.fromarray expects a uint array
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)
Works on both uint and float tensors
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
with tf.Session() as sess:
out = sess.run(tf.image.convert_image_dtype(central_crop_image,tf.uint8))
print(out.shape)
Image.fromarray(out)
Works on both uint and float tensors
flip_image = tf.image.flip_up_down(float_resized_image)
#flip_image = tf.image.flip_up_down(uint_resized_image)
print(flip_image)
with tf.Session() as sess:
out = sess.run(tf.image.convert_image_dtype(flip_image,tf.uint8))
print(out.shape)
Image.fromarray(out)
For all color adjustment operations like brightness, contrast, saturation, hue, tensorflow expects uint image. Does NOT work on float images
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)
with tf.Session() as sess:
out = sess.run(tf.image.convert_image_dtype(bright_image,tf.uint8))
print(out.shape)
Image.fromarray(out)
grey_image = tf.image.rgb_to_grayscale(image_tensor)
grey_image
with tf.Session() as sess:
out = sess.run(grey_image)
print(out.shape)
Image.fromarray(out.reshape(395,500))