kaishi.image.ops

Definitions for common operations on images.

Module Contents

kaishi.image.ops.extract_patch(im, patch_size)

Extract a center cropped patch of size ‘patch_size’ (2-element tuple).

Parameters
  • im (PIL image object) – input image

  • patch_size (tuple, array, or similar) – size of patch

Returns

center-cropped patch

Return type

PIL image object

kaishi.image.ops.make_small(im, max_dim: int = 224, resample_method=Image.NEAREST)

Make a small version of an image while maintaining aspect ratio.

Parameters
  • im (PIL image object) – input image

  • max_dim (int) – maximum dimension of resized image (x or y)

  • resample_method (PIL resample method) – method for resampling the image

Returns

resized image

Return type

PIL image object

kaishi.image.ops.add_jpeg_compression(im, quality_level: int = 30)

Apply JPEG compression to an image with a given quality level.

Parameters
  • im (PIL image object) – input image

  • quality_level (int) – JPEG qualit level, where: 0 < value <= 100

Returns

compressed image

Return type

PIL image object

kaishi.image.ops.add_rotation(im, ccw_rotation_degrees: int = 90)

Rotate an image CCW by ccw_rotation_degrees degrees.

Parameters
  • im (PIL image object) – input image

  • ccw_rotation_degrees (int) – number of degrees to rotate counter-clockwise

Returns

rotated image

Return type

PIL image object

kaishi.image.ops.add_stretching(im, w_percent_additional, h_percent_additional)

Stretch an image by the specified percentages.

Parameters
  • im (PIL image object) – input image

  • w_percent_additional (int or float greater than 0) – amount of width stretching to add (0 maintains the same size, 100 doubles the size)

  • h_percent_additional (int or float greater than 0) – amount of height stretching to add (0 maintains the same size, 100 doubles the size)

Returns

stretched image

Return type

PIL image object

kaishi.image.ops.add_poisson_noise(im, param: float = 1.0, rescale: bool = True)

Add Poisson noise to image, where (poisson noise * param) is the final noise function.

See http://kmdouglass.github.io/posts/modeling-noise-for-image-simulations for more info. If rescale is set to True, the image will be rescaled after noise is added. Otherwise, the noise will saturate.

Parameters
  • im (PIL image object) – input image

  • param (float) – noise parameter

  • rescale (bool) – flag indicating whether or not to rescale the image after adding noise (maintaining original image extrema)

Returns

image with Poisson noise added

Return type

PIL image object