Utils

A set of utility functions. All of the functions can be imported from kiui directly.

Examples

import kiui

x = kiui.read_image('image.png')
kiui.lo(x)

API

kiui.utils.lo(*xs, verbose=0)[source]

inspect array like objects and report statistics.

Parameters:
  • xs (Any) – array like objects to inspect.

  • verbose (int, optional) – level of verbosity, set to 1 to report mean and std, 2 to print the content. Defaults to 0.

kiui.utils.seed_everything(seed=42, verbose=False, strict=False)[source]

auto set seed for random, numpy and torch.

Parameters:
  • seed (int, optional) – random seed. Defaults to 42.

  • verbose (bool, optional) – whether to report each seed setting. Defaults to False.

  • strict (bool, optional) – whether to use strict deterministic mode for better torch reproduction. Defaults to False.

kiui.utils.read_json(path)[source]

load a json file.

Parameters:

path (str) – path to json file.

Returns:

json content.

Return type:

dict

kiui.utils.write_json(path, x)[source]

write a json file.

Parameters:
  • path (str) – path to write json file.

  • x (dict) – dict to write.

kiui.utils.read_pickle(path)[source]

read a pickle file.

Parameters:

path (str) – path to pickle file.

Returns:

pickle content.

Return type:

Any

kiui.utils.write_pickle(path, x)[source]

write a pickle file.

Parameters:
  • path (str) – path to write pickle file.

  • x (Any) – content to write.

kiui.utils.read_image(path: str, mode: Literal[‘float’, ‘uint8’, ‘pil’, ‘torch’, ‘tensor’] = 'float', order: Literal[‘RGB’, ‘RGBA’, ‘BGR’, ‘BGRA’] = 'RGB')[source]

read an image file into various formats and color mode.

Parameters:
  • path (str) – path to the image file.

  • mode (Literal[“float”, “uint8”, “pil”, “torch”, “tensor”], optional) – returned image format. Defaults to “float”. float: float32 numpy array, range [0, 1]; uint8: uint8 numpy array, range [0, 255]; pil: PIL image; torch/tensor: float32 torch tensor, range [0, 1];

  • order (Literal[“RGB”, “RGBA”, “BGR”, “BGRA”], optional) – channel order. Defaults to “RGB”.

Note

By default this function will convert RGBA image to white-background RGB image. Use order="RGBA" to keep the alpha channel.

Returns:

the image array.

Return type:

Union[np.ndarray, PIL.Image, torch.Tensor]

kiui.utils.write_image(path: str, img: Tensor | ndarray | Image, order: Literal[‘RGB’, ‘BGR’] = 'RGB')[source]

write an image to various formats.

Parameters:
  • path (str) – path to write the image file.

  • img (Union[torch.Tensor, np.ndarray, PIL.Image.Image]) – image to write.

  • order (str, optional) – channel order. Defaults to “RGB”.

kiui.utils.load_file_from_url(url, model_dir=None, progress=True, file_name=None)[source]

Load file form http url, will download models if necessary.

Parameters:
  • url (str) – URL to be downloaded.

  • model_dir (str) – The path to save the downloaded model. Should be a full path. If None, use pytorch hub_dir. Default: None.

  • progress (bool) – Whether to show the download progress. Default: True.

  • file_name (str) – The downloaded file name. If None, use the file name in the url. Default: None.

Returns:

The path to the downloaded file.

Return type:

str

kiui.utils.is_format(f: str, format: Sequence[str])[source]

if a file’s extension is in a set of format

Parameters:
  • f (str) – file name.

  • format (Sequence[str]) – set of extensions (both ‘.jpg’ or ‘jpg’ is ok).

Returns:

if the file’s extension is in the set.

Return type:

bool

kiui.utils.batch_process_files(process_fn, path, out_path, overwrite=False, in_format=['.jpg', '.jpeg', '.png'], out_format=None, image_mode='uint8', image_color_order='RGB', **kwargs)[source]

simple function wrapper to batch processing files.

Parameters:
  • process_fn (Callable) – process function.

  • path (str) – path to a file or a directory containing the files to process.

  • out_path (str) – output path of a file or a directory.

  • overwrite (bool, optional) – whether to overwrite existing results. Defaults to False.

  • in_format (list, optional) – input file formats. Defaults to [“.jpg”, “.jpeg”, “.png”].

  • out_format (str, optional) – output file format. Defaults to None.

  • image_mode (str, optional) – for images, the mode to read. Defaults to ‘uint8’.

  • image_color_order (str, optional) – for images, the color order. Defaults to “RGB”.