fastvideo.v1.models.vision_utils
#
Module Contents#
Functions#
Returns the height and width of the image, downscaled to the next integer multiple of |
|
Loads |
|
Normalize an image array to [-1,1]. |
|
Convert a NumPy image to a PyTorch tensor. |
|
Convert a PIL image or a list of PIL images to NumPy arrays. |
|
Resize image. |
API#
- fastvideo.v1.models.vision_utils.get_default_height_width(image: Union[PIL.Image.Image, numpy.ndarray, torch.Tensor], vae_scale_factor: int, height: Optional[int] = None, width: Optional[int] = None) Tuple[int, int] [source]#
Returns the height and width of the image, downscaled to the next integer multiple of
vae_scale_factor
.- Parameters:
image (
Union[PIL.Image.Image, np.ndarray, torch.Tensor]
) – The image input, which can be a PIL image, NumPy array, or PyTorch tensor. If it is a NumPy array, it should have shape[batch, height, width]
or[batch, height, width, channels]
. If it is a PyTorch tensor, it should have shape[batch, channels, height, width]
.height (
Optional[int]
, optional, defaults toNone
) – The height of the preprocessed image. IfNone
, the height of theimage
input will be used.width (
Optional[int]
, optional, defaults toNone
) – The width of the preprocessed image. IfNone
, the width of theimage
input will be used.
- Returns:
A tuple containing the height and width, both resized to the nearest integer multiple of
vae_scale_factor
.- Return type:
Tuple[int, int]
- fastvideo.v1.models.vision_utils.load_image(image: Union[str, PIL.Image.Image], convert_method: Optional[Callable[[PIL.Image.Image], PIL.Image.Image]] = None) PIL.Image.Image [source]#
Loads
image
to a PIL Image.- Parameters:
image (
str
orPIL.Image.Image
) – The image to convert to the PIL Image format.convert_method (Callable[[PIL.Image.Image], PIL.Image.Image], optional) – A conversion method to apply to the image after loading it. When set to
None
the image will be converted “RGB”.
- Returns:
A PIL Image.
- Return type:
PIL.Image.Image
- fastvideo.v1.models.vision_utils.normalize(images: Union[numpy.ndarray, torch.Tensor]) Union[numpy.ndarray, torch.Tensor] [source]#
Normalize an image array to [-1,1].
- Parameters:
images (
np.ndarray
ortorch.Tensor
) – The image array to normalize.- Returns:
The normalized image array.
- Return type:
np.ndarray
ortorch.Tensor
- fastvideo.v1.models.vision_utils.numpy_to_pt(images: numpy.ndarray) torch.Tensor [source]#
Convert a NumPy image to a PyTorch tensor.
- Parameters:
images (
np.ndarray
) – The NumPy image array to convert to PyTorch format.- Returns:
A PyTorch tensor representation of the images.
- Return type:
torch.Tensor
- fastvideo.v1.models.vision_utils.pil_to_numpy(images: Union[List[PIL.Image.Image], PIL.Image.Image]) numpy.ndarray [source]#
Convert a PIL image or a list of PIL images to NumPy arrays.
- Parameters:
images (
PIL.Image.Image
orList[PIL.Image.Image]
) – The PIL image or list of images to convert to NumPy format.- Returns:
A NumPy array representation of the images.
- Return type:
np.ndarray
- fastvideo.v1.models.vision_utils.resize(image: Union[PIL.Image.Image, numpy.ndarray, torch.Tensor], height: int, width: int, resize_mode: str = 'default', resample: str = 'lanczos') Union[PIL.Image.Image, numpy.ndarray, torch.Tensor] [source]#
Resize image.
- Parameters:
image (
PIL.Image.Image
,np.ndarray
ortorch.Tensor
) – The image input, can be a PIL image, numpy array or pytorch tensor.height (
int
) – The height to resize to.width (
int
) – The width to resize to.resize_mode (
str
, optional, defaults todefault
) – The resize mode to use, can be one ofdefault
orfill
. Ifdefault
, will resize the image to fit within the specified width and height, and it may not maintaining the original aspect ratio. Iffill
, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, filling empty with data from image. Ifcrop
, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess. Note that resize_modefill
andcrop
are only supported for PIL image input.
- Returns:
The resized image.
- Return type:
PIL.Image.Image
,np.ndarray
ortorch.Tensor