Image Utils Example

This example demonstrates the usage of the tensorlight.utils.image module.


In [ ]:
# Force matplotlib to use inline rendering
%matplotlib inline

import os
import sys

# add path to libraries for ipython
sys.path.append(os.path.expanduser("~/libs"))

import cv2
import numpy as np
import tensorlight as light

Read images


In [ ]:
img_color = light.utils.image.read("assets/color250x250.jpg")
img_gray = light.utils.image.read("assets/color250x250.jpg", cv2.IMREAD_GRAYSCALE)

print(img_color.shape, img_color.dtype)
print(img_gray.shape, img_gray.dtype)

light.visualization.display_array(img_color)
light.visualization.display_array(img_gray)

light.visualization.display_batch(np.expand_dims(img_color, 0))
light.visualization.display_batch(np.expand_dims(img_gray, 0))

In [ ]:
img_color = light.utils.image.read("assets/example250x250.jpg")
img_gray = light.utils.image.read("assets/example250x250.jpg", cv2.IMREAD_GRAYSCALE)

img_color64 = img_color / 255.0
img_color32 = np.float32(img_color / 255.0)

print(img_color32.shape, img_color32.dtype)
print(img_color64.shape, img_color64.dtype)
print(img_gray.shape, img_gray.dtype)

light.visualization.display_array(img_color32)
light.visualization.display_array(img_color64)
light.visualization.display_array(img_gray)

light.visualization.display_batch(np.expand_dims(img_color32, 0))
light.visualization.display_batch(np.expand_dims(img_color64, 0))
light.visualization.display_batch(np.expand_dims(img_gray, 0))

Conversion


In [ ]:
color_to_gray_img = light.utils.image.to_grayscale(img_color)
print(color_to_gray_img.shape, color_to_gray_img.dtype)

light.visualization.display_array(color_to_gray_img)

In [ ]:
gray_to_color_img = light.utils.image.to_rgb(img_gray)
print(gray_to_color_img.shape, gray_to_color_img.dtype)

light.visualization.display_array(gray_to_color_img)

Resize


In [ ]:
img_color_resized = light.utils.image.resize(img_color, 0.5)
print(img_color_resized.shape, img_color_resized.dtype)
img_gray_resized = light.utils.image.resize(img_gray, size=(50, 100))
print(img_gray_resized.shape, img_gray_resized.dtype)
img_gray_resized_odd = light.utils.image.resize(img_gray_resized, 0.333333)
print(img_gray_resized_odd.shape, img_gray_resized_odd.dtype)

light.visualization.display_array(img_color_resized)
light.visualization.display_array(img_gray_resized)
light.visualization.display_array(img_gray_resized_odd)

Save images


In [ ]:
print(img_color_resized.shape, img_color_resized.dtype)
img_color_resized_float32 = np.float32(img_color_resized / 255.0)
print(img_color_resized.shape, img_color_resized.dtype)
print(img_color_resized_float32.shape, img_color_resized_float32.dtype)
print(img_gray.shape, img_gray.dtype)

light.utils.image.write("out/img_color_resized_uint8.png",
                     img_color_resized)
light.utils.image.write("out/img_color_resized_float32.png",
                     img_color_resized_float32)
light.utils.image.write_as_binary("out/img_gray.bin",
                               img_gray)

Pad or crop


In [ ]:
img = light.utils.image.read("assets/color250x250.jpg")
print("Original", img.shape)
img_same = light.utils.image.pad_or_crop(img, img.shape)
print("Same", img_same.shape)

img_odd_pad = light.utils.image.pad_or_crop(img, [251, 251])
print("Odd pad", img_odd_pad.shape)
print("Original unchanged", img.shape)
img_even_pad = light.utils.image.pad_or_crop(img, [300, 300], pad_value=0)
print("Even pad", img_even_pad.shape)
print("Original unchanged", img.shape)

img_odd_crop = light.utils.image.pad_or_crop(img, [249, 249])
print("Odd crop", img_odd_crop.shape)
print("Original unchanged", img.shape)
img_even_crop = light.utils.image.pad_or_crop(img, [200, 200], pad_value=0)
print("Even crop", img_even_crop.shape)
print("Original unchanged", img.shape)

img_mixed = light.utils.image.pad_or_crop(img, [300, 200], pad_value=0)
print("Mixed", img_mixed.shape)
print("Original unchanged", img.shape)

light.visualization.display_array(img)
light.visualization.display_array(img_same)
light.visualization.display_array(img_even_pad)
light.visualization.display_array(img_even_crop)
light.visualization.display_array(img_mixed)

# test that modifying the copy does not change the original
img_even_pad.fill(99)
img_odd_pad.fill(99)
img_even_crop.fill(99)
img_odd_crop.fill(99)
img_reloaded = light.utils.image.read("assets/color250x250.jpg")
print("Check that image manipulation did not change the original:",
      np.all(img == img_reloaded))

In [ ]: