<jupyter_start><jupyter_text>Image Utils ExampleThis example demonstrates the usage of the tensorlight.utils.image module.<jupyter_code># 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<jupyter_output><empty_output><jupyter_text>Read images<jupyter_code>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))
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))<jupyter_output><empty_output><jupyter_text>Conversion<jupyter_code>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)
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)<jupyter_output><empty_output><jupyter_text>Resize<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Save images<jupyter_code>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)<jupyter_output><empty_output><jupyter_text>Pad or crop<jupyter_code>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))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11451342281879194, 'fraction_numerical': 0.030620805369127518, 'mean_word_length': 7.964285714285714, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '660538', 'license': 'MIT', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1752, 'n_words': 219, 'path': 'examples/image-utils.ipynb', 'repo_name': 'bsautermeister/tensorlight', 'size': '4768'}