# *************************************************************************** # # # # image.py # # # # By: Widium # # Github : https://github.com/widium # # # # Created: 2022/11/10 08:51:01 by ebennace # # Updated: 2023/05/03 16:05:48 by Widium # # # # **************************************************************************** # import tensorflow as tf import numpy as np from tensorflow import Tensor from PIL import Image from cv2 import cvtColor from cv2 import imread from cv2 import COLOR_BGR2RGB from .processing import Normalize_image from .processing import inverse_normalize_image from .processing import remove_batch_dimension # ======================================== # def load_image_path(path : str): """ Load and preprocess the color of imag with OpenCV Args: path (str): filepath of image Returns: np.array: img in Numpy Array Format """ img = imread(path) img = cvtColor(img, COLOR_BGR2RGB) img = Normalize_image(img) return (img) # ======================================== # def tensor_to_image(tensor : Tensor): """ Convert a tensor to an image in PIL format. Args: tensor: The input image as a tensor. Returns: Image: The converted image in PIL format. """ tensor = inverse_normalize_image(tensor) array = np.array(tensor, dtype=np.uint8) array = remove_batch_dimension(array) img_pil = Image.fromarray(array) return img_pil # ======================================== # def clip_pixel(image : Tensor): """ Clip pixel values of an image tensor between 0 and 1. Args: image: The input image as a tensor. Returns: Tensor: The clipped image tensor. """ cliped_image = tf.clip_by_value( t=image, clip_value_min=0.0, clip_value_max=1.0 ) return (cliped_image) # ======================================== # def create_noisy_imag(img : Tensor): """ Create Noisy image with Random pixel with same shape of input img Args: img: The input image as a tensor. Returns: np.ndarray: The noisy image as a NumPy array. """ noise_img = np.random.randn(*img.shape) return (noise_img) # ===================================================== #