mash.images package#

Submodules#

mash.images.conversion module#

mash.images.conversion.pil_from_uri(uri: str) Image#

Return a PIL image from an image file or URL.

Parameters:

uri – File path or url to the image.

Returns:

PIL image.

mash.images.conversion.to_numpy(image: str | ndarray | Image | Tensor) ndarray#

Create a numpy array from a variety of input types.

Parameters:

input – Input to convert to a numpy array, can be a file path or an array.

Returns:

Numpy array, either uint8 for PIL images or the same type as the input.

mash.images.conversion.to_pil(image: str | ndarray | Image | Tensor) Image#

Create a PIL Image from a variety of input types.

Parameters:

input – Input to convert to a numpy array, can be a file path or an array.

Returns:

PIL image, if the input is not uint8 it will be assumed to be 0-1 and scaled.

mash.images.conversion.to_tensor(image: str | ndarray | Image | Tensor) Tensor#

Create a torch.Tensor from a variety of input types.

Parameters:

input – Input to convert to a tensor, can be a file path or an array.

Returns:

PyTorch Tensor, either uint8 for PIL images or the same type as the input.

mash.images.crop module#

mash.images.crop.center_square_crop(image: ndarray, crop_size: int, return_rgb: bool = False) ndarray#

Crop the center of the image to the specified size.

Parameters:
  • image – The image to crop.

  • crop_size – The size of the crop.

  • return_rgb – Return in RGB format instead of the input format.

Returns:

The cropped image.

mash.images.crop.crop_rectangle(image: ndarray, crop_height: int, crop_width: int, start_x: int, start_y: int, return_rgb: bool = False) ndarray#

Crop a rectangle from the image.

Parameters:
  • image – The image to crop, either grayscale or RGB/A.

  • crop_height – The height of the crop.

  • crop_width – The width of the crop.

  • start_x – The x-coordinate of the top-left corner of the crop.

  • start_y – The y-coordinate of the top-left corner of the crop.

  • return_rgb – Return in RGB format instead of the input format.

Returns:

The cropped image.

mash.images.crop.crop_square(image: ndarray, crop_size: int, start_x: int, start_y: int, return_rgb: bool = False) ndarray#

Crop a square from the image.

Parameters:
  • image – The image to crop.

  • crop_size – The side length of the crop.

  • start_x – The x-coordinate of the top-left corner of the crop.

  • start_y – The y-coordinate of the top-left corner of the crop.

  • return_rgb – Return in RGB format instead of the input format.

Returns:

The cropped image.

mash.images.crop.crop_to_multiple_of_dimension(img: ndarray, multiple: int) ndarray#

Crop an image to a multiple of a dimension. Useful for models that require input dimensions that are multiples of a certain number, i.e. ViT models.

Parameters:
  • img – The image to crop.

  • multiple – The multiple of each dimension to crop to.

Returns:

The cropped image, centered in the original image.

mash.images.crop.random_square_crop(image: ndarray, crop_size: int, return_rgb: bool = False) ndarray#

Crop a random square from the image.

Parameters:
  • image – The image to crop.

  • crop_size – The side length of the crop.

  • return_rgb – Return in RGB format instead of the input format.

Returns:

The cropped image.

mash.images.normalization module#

mash.images.normalization.standardize(image: ndarray, dataset: str | None = None, mean: ndarray | None = None, std: ndarray | None = None)#

Standardize an image.

Parameters:
  • image – Image (numpy) to standardize.

  • dataset – Dataset to use for standardization. Defaults to “imagenet”.

  • mean – Mean to use for standardization. Defaults to None, overrides dataset string.

  • std – Standard deviation to use for standardization. Defaults to None, overrides dataset string.

Returns:

Standardized image.

Return type:

np.ndarray

mash.images.resize module#

mash.images.resize.resize_image_max_side(image: ndarray, max_side_len: int = 224, preserve_range: bool = False) ndarray#

Resize the image such that the longest side is equal to the specified length.

Parameters:
  • image – The image to resize.

  • max_side_len – The length of the smallest side.

  • preserve_range – Preserve the range of the image.

Returns:

The resized image.

mash.images.resize.resize_image_min_side(image: ndarray, min_side_len: int = 224, preserve_range: bool = False) ndarray#

Resize the image such that the smallest side is equal to the specified length.

Parameters:
  • image – The image to resize.

  • min_side_len – The length of the smallest side.

  • preserve_range – Preserve the range of the image.

Returns:

The resized image.

mash.images.tile module#

mash.images.tile.image_to_tiles(image: ndarray, tile_width: int, tile_height: int, overlap_width: int | None = None, overlap_height: int | None = None) ndarray#

Splits an image into tiles with optional overlap.

Parameters:
  • image – A numpy array representing the image.

  • tile_width – The width of each tile.

  • tile_height – The height of each tile.

  • overlap_width – The overlap between tiles horizontally.

  • overlap_height – The overlap between tiles vertically.

Returns:

A numpy array containing the tiles. Each tile is a sub-array of the original image.

mash.images.truecolor module#

mash.images.truecolor.grayscale_to_rgb(image: ndarray) ndarray#

Convert a grayscale image to RGB by duplicating the channels.

Parameters:

image – The grayscale image.

Returns:

The RGB image.

mash.images.truecolor.transparent_to_rgb(image: ndarray) ndarray#

Convert a transparent image to RGB by dropping the transparency.

Parameters:

image – The transparent image.

Returns:

The RGB image.

Module contents#