# Licensed under the TENCENT HUNYUAN COMMUNITY LICENSE AGREEMENT (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://github.com/Tencent-Hunyuan/HunyuanVideo-1.5/blob/main/LICENSE # # Unless and only to the extent required by applicable law, the Tencent Hunyuan works and any # output and results therefrom are provided "AS IS" without any express or implied warranties of # any kind including any warranties of title, merchantability, noninfringement, course of dealing, # usage of trade, or fitness for a particular purpose. You are solely responsible for determining the # appropriateness of using, reproducing, modifying, performing, displaying or distributing any of # the Tencent Hunyuan works or outputs and assume any and all risks associated with your or a # third party's use or distribution of any of the Tencent Hunyuan works or outputs and your exercise # of rights and permissions under this agreement. # See the License for the specific language governing permissions and limitations under the License. import torch from typing import List import numpy as np from PIL import Image def numpy_to_pil(images: np.ndarray) -> List[Image.Image]: """ Convert a numpy image or a batch of images to a PIL image. Args: images (np.ndarray): The image array to convert to PIL format. Returns: List[Image.Image]: A list of PIL images. """ if images.ndim == 3: images = images[None, ...] images = (images * 255).round().astype("uint8") if images.shape[-1] == 1: # special case for grayscale (single channel) images pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] else: pil_images = [Image.fromarray(image) for image in images] return pil_images def merge_tensor_by_mask(tensor_1, tensor_2, mask, dim): assert tensor_1.shape == tensor_2.shape # Mask is a 0/1 vector. Choose tensor_2 when the value is 1; otherwise, tensor_1 masked_indices = torch.nonzero(mask).squeeze(1) tmp = tensor_1.clone() if dim == 0: tmp[masked_indices] = tensor_2[masked_indices] elif dim == 1: tmp[:, masked_indices] = tensor_2[:, masked_indices] elif dim == 2: tmp[:, :, masked_indices] = tensor_2[:, :, masked_indices] return tmp