grains.simulation.change_domain

grains.simulation.change_domain(image, left, right, bottom, top, padding_value=nan)[source]

Extends or crops the domain an image fills.

The original image is extended, cropped or left unchanged in the left, right, bottom and top directions by padding the corresponding 2D or 3D array with a given value or removing existing elements. Non-integer image length is avoided by rounding up. This choice prevents ending up with an empty image during cropping or no added pixel during extension.

Parameters
  • image (ndarray) – 2D or 3D numpy array representing the original image.

  • left, right (float) – When positive, the domain is extended, when negative, the domain is cropped by the given value relative to the image width.

  • bottom, top (float) – When positive, the domain is extended, when negative, the domain is cropped by the given value relative to the image height.

  • padding_value (float, optional) – Value for the added pixels. The default is numpy.nan.

Returns

changed_image (ndarray) – 2D or 3D numpy array representing the modified domain.

Examples

Crop an image at the top, extended it at the bottom and on the left, and leave it unchanged on the right. Note the rounding for non-integer pixels.

>>> import numpy as np
>>> image = np.array([[0.1, 0.2, 0.3], [0.4, 0.5, 0.6], [0.7, 0.8, 0.9]])
>>> modified = change_domain(image, 0.5, 0, 1/3, -1/3, 0)
>>> image  # no in-place modification, the original image is not overwritten
array([[0.1, 0.2, 0.3],
       [0.4, 0.5, 0.6],
       [0.7, 0.8, 0.9]])
>>> modified
array([[0. , 0. , 0.4, 0.5, 0.6],
       [0. , 0. , 0.7, 0.8, 0.9],
       [0. , 0. , 0. , 0. , 0. ]])