pyvista.ImageDataFilters.slice_index

pyvista.ImageDataFilters.slice_index#

ImageDataFilters.slice_index(
i: int | VectorLike[int] | slice | None = None,
j: int | VectorLike[int] | slice | None = None,
k: int | VectorLike[int] | slice | None = None,
*,
index_mode: Literal['extent', 'dimensions'] = 'dimensions',
strict_index: bool = False,
rebase_coordinates: bool = False,
progress_bar: bool = False,
) ImageData[source]#

Extract a subset using IJK indices.

This filter enables slicing ImageData with Python-style indexing using IJK coordinates. It can be used to extract a single slice, multiple contiguous slices, or a volume of interest. Unlike other slicing filters, this filter returns ImageData.

Note

Slicing by index is also possible using the “get index” operator []. See examples.

Parameters:
i, j, kint | VectorLike[int] | slice, optional

Indices to slice along the I, J, and K coordinate axes, respectively. Specify an integer for a single index, or two integers [start, stop) for a range of indices.

Note

Like regular Python slicing:

  • Half-open intervals are used, i.e. the start index is included in the range but the stop index is not.

  • Negative indexing is supported.

  • An IndexError is raised when a single integer is specified as the index and the index is out-of-bounds.

  • An IndexError is not raised when a range is specified as the index and the index is out-of-bounds. This default can be overridden by setting strict_index=True.

  • A copy of the data is returned (modifying the sliced output does not affect the input data).

index_mode‘extent’ | ‘dimensions’, default: ‘dimensions’

Mode to use when determining the range of values to index from.

  • Use 'dimensions' to index values in the range [0, dimensions - 1].

  • Use 'extent' to index values based on the extent, i.e. [offset, offset + dimensions - 1].

The main difference between these modes is the inclusion or exclusion of the offset. dimensions is more pythonic and is how the object’s data arrays themselves would be indexed, whereas 'extent' respects VTK’s definition of extent and considers the object’s geometry.

strict_indexbool, default: False

Raise an IndexError if any of the indices are out of range. By default, an IndexError is only raised if a single integer index is out of range, but not when a range of indices are specified; set this to True to raise in error in both cases.

rebase_coordinatesbool, default: False

Rebase the coordinate reference of the extracted subset:

  • the origin is set to the minimum bounds of the subset

  • the offset is reset to (0, 0, 0)

The rebasing effectively applies a positive translation in world (XYZ) coordinates and a similar (i.e. inverse) negative translation in voxel (IJK) coordinates. As a result, the bounds of the output are unchanged, but the coordinate reference frame is modified.

Set this to False to leave the origin unmodified and keep the offset specified by the indexing.

progress_barbool, default: False

Display a progress bar to indicate progress.

Returns:
ImageData

Sliced mesh.

Examples

Create a ImageData mesh and give it some point data.

>>> import pyvista as pv
>>> mesh = pv.ImageData(dimensions=(10, 10, 10))
>>> mesh['data'] = range(mesh.n_points)

Extract a single slice along the k-axis.

>>> sliced = mesh.slice_index(k=5)
>>> sliced.dimensions
(10, 10, 1)

Equivalently:

>>> sliced2 = mesh[:, :, 5]
>>> sliced == sliced2
True

Extract a volume of interest.

>>> sliced = mesh.slice_index(i=[1, 3], j=[2, 5], k=[5, 10])
>>> sliced.dimensions
(2, 3, 5)

Equivalently:

>>> sliced2 = mesh[1:3, 2:5, 5:10]
>>> sliced == sliced2
True

Use None to implicitly define the start and/or stop indices.

>>> sliced = mesh.slice_index(i=[None, 3], j=[2, None], k=None)
>>> sliced.dimensions
(3, 8, 10)

Equivalently:

>>> sliced2 = mesh[:3, 2:, :]
>>> sliced == sliced2
True

See Slicing for more examples using this filter.