ddd_pylib.image_manip.

CropOperator#

class ddd_pylib.image_manip.CropOperator[source]#

Bases: ImageOperator

Takes a list of requested crops and extracts them from the main image. Each crop is a dictionary with:

  • keys (str): The axes to crop (e.g. “X”, “Y”, “Z”, “T”)

  • values (tuple[int, int]): The start and end points of the crop along that axis

The tuples follow the same logic as Python slicing, where the start is inclusive and the end is exclusive. For example, a crop of (0, 100) along the X axis will include pixels from 0 to 99.

Example of a crop list:

[
    {"X": (0, 100), "Y": (20, 200), "Z": (0, 10), "T": (2, 3)},
    {"X": (100, 200), "Y": (0, 200), "Z": (0, 10)}
]

If a crop dict is not mentioning an axis, the whole axis is taken (for example, if ‘T’ is missing, the whole time series will be exported). A crop with an empty dict will take the whole image. The result is a list of tuples with:

  • the first element of the tuples represents the translation of the cropped image in the original image (the starting point of the crop)

  • the second element of the tuples is the cropped image as an xr.DataArray

Settable parameters:
  • mainImage (setMainImage) – The main image to crop

  • crops (setCropsList) – The list of crops to apply to the image

Produces:

result (getResult) – A list of tuples with the translation and the cropped image

Example

from ddd_pylib.image_manip import CropOperator
from ddd_pylib import ImageUtils
from tifffile import imread, imwrite
from pathlib import Path
import xarray as xr

root = Path("data-ddd-pylib")
im_name = "track_me_full-1.tif"
im = xr.DataArray(
    imread(root / im_name),
    dims=["T", "Z", "Y", "X"], # (3, 30, 349, 639)
    name=im_name,
)

cropsList = [
    {'T': (0, 1), 'Z': (6, 30), 'Y': (154, 222), 'X': (120, 198)},
    {'T': (2, 3),               'Y': (138, 210), 'X': (472, 532)}, # Whole Z axis
]

op = CropOperator()
op.setMainImage(im)
op.setCropsList(cropsList)
op.run()

res = op.getResult()
for rank, (_, cropped) in enumerate(res):
    ImageUtils.writeTiff(
        root / "results",
        cropped
    )

Methods

getPrefix()[source]#

Prefix added to the input image name to generate the output image name.

Return type:

str

sanityCheck()[source]#

Verifies that the requested crops are valid:

  • The start point must be less than the end point

  • The start point must be non-negative

  • The end point must be less than or equal to the size of the image along that axis

setCropsList(crops)[source]#

Sets the list of crops that is needed to get from the image.

Parameters:

crops (List[dict]) – List of dictionaries that define crops.