hezar.utils.image_utils module

hezar.utils.image_utils.convert_image_type(image: ndarray | Image | Tensor, target_type: str | ImageType = ImageType.NUMPY)[source]

Convert image lib type. Supports numpy array, pillow image and torch tensor.

hezar.utils.image_utils.crop_boxes(image, bboxes, padding: int | tuple | None = None) list[Image.Image][source]

Crop all bounding boxes in an image

Parameters:
  • image – A single image (Pillow Image object)

  • bboxes – A list of bboxes in a single image (x1, y1, width, height)

  • padding – Number of pixels to pad the bbox coordinates. An int value means all sides and tuple pads sides specifically.

Returns:

A list of cropped images

hezar.utils.image_utils.draw_boxes(image, bboxes, bbox_color: tuple = (0, 255, 0)) Image[source]

Draw bbox on the image

Parameters:
  • image – A single image (Pillow Image object)

  • bboxes – A list of bboxes in a single image

  • bbox_color – Color of the bbox in image (RGB tuple)

Returns:

The overlaid image

hezar.utils.image_utils.find_channels_axis_side(image: ndarray, num_channels: int | None = None)[source]
hezar.utils.image_utils.gray_scale_image(image: ndarray, return_type: str | ImageType = ImageType.NUMPY)[source]
hezar.utils.image_utils.load_image(path, return_type: str | ImageType = ImageType.PILLOW)[source]

Load an image file to a desired return format

Parameters:
  • path – Path to image file

  • return_type – Image output type (“pillow”, “numpy”, “torch”)

Returns:

The desired output image of type PIL.Image or numpy.ndarray or torch.Tensor

hezar.utils.image_utils.mirror_image(image: ndarray, return_type: str | ImageType = ImageType.NUMPY)[source]
hezar.utils.image_utils.normalize_image(image: ndarray, mean: float | Iterable[float], std: float | Iterable[float], channel_axis: str | ChannelsAxisSide = 'first')[source]
hezar.utils.image_utils.pad_boxes(bboxes, padding: int | tuple | None = None)[source]

Add a padding to sides of the bounding boxes.

Parameters:
  • bboxes – A list of bounding boxes (x1, y1, w, h)

  • padding – A single integer value to pad equally or a tuple of size 4 for more specific padding. Tuple order is (left, up, right, down)

Returns:

A list of padded bounding boxes

hezar.utils.image_utils.rescale_image(image: ndarray, scale: float)[source]
hezar.utils.image_utils.resize_image(image: ndarray, size: Tuple[int, int], resample=None, reducing_gap: float | None = None, return_type: ImageType = ImageType.NUMPY)[source]

Resize a numpy array image (actually uses pillow PIL.Image.resize(…))

Parameters:
  • image – Numpy image

  • size – A tuple of (width, height)

  • resample – Resampling filter (refer to PIL.Image.Resampling) for possible values

  • reducing_gap – Optimization method for resizing based on reducing times

  • return_type – Return type of the image (numpy, torch, pillow)

Returns:

The resized image

hezar.utils.image_utils.save_image(image: Image | Tensor | ndarray, path: str)[source]

Save a PIL/numpy/torch image into a path.

Parameters:
  • image – A PIL.Image or np.ndarray or torch.Tensor image type

  • path – The full path of the image to save

hezar.utils.image_utils.show_image(image: Image | Tensor | ndarray, title: str = 'Image')[source]

Given any type of input image (PIL, numpy, torch), show the image in a window

Parameters:
  • image – Input image of types PIL.Image, numpy.ndarray or torch.Tensor

  • title – Optional title for the preview window

hezar.utils.image_utils.transpose_channels_axis_side(image: ndarray, axis_side: str | ChannelsAxisSide, num_channels: int | None = None, src_axis_side: str | ChannelsAxisSide | None = None)[source]

Convert an image channels axis side from (channels, …) to (…, channels) or vise versa.

Parameters:
  • image – Input image

  • axis_side – The desired axis side (can be “first” or “last”)

  • num_channels – The number of channels in the input image

  • src_axis_side – The image initial channels axis side (can be “first” or “last”)

Returns:

The image with the converted channels axis