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,
Extract a subset using IJK indices.
This filter enables slicing
ImageDatawith 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 returnsImageData.Note
Slicing by index is also possible using the “get index” operator
[]. See examples.- Parameters:
- i, j, k
int|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
startindex is included in the range but thestopindex is not.Negative indexing is supported.
An
IndexErroris raised when a single integer is specified as the index and the index is out-of-bounds.An
IndexErroris not raised when a range is specified as the index and the index is out-of-bounds. This default can be overridden by settingstrict_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 theextent, i.e.[offset, offset + dimensions - 1].
The main difference between these modes is the inclusion or exclusion of the
offset.dimensionsis more pythonic and is how the object’s data arrays themselves would be indexed, whereas'extent'respects VTK’s definition ofextentand considers the object’s geometry.- strict_indexbool, default:
False Raise an
IndexErrorif any of the indices are out of range. By default, anIndexErroris only raised if a single integer index is out of range, but not when a range of indices are specified; set this toTrueto raise in error in both cases.- rebase_coordinatesbool, default:
False Rebase the coordinate reference of the extracted subset:
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
boundsof the output are unchanged, but the coordinate reference frame is modified.Set this to
Falseto leave the origin unmodified and keep the offset specified by the indexing.- progress_barbool, default:
False Display a progress bar to indicate progress.
- i, j, k
- Returns:
ImageDataSliced mesh.
See also
Examples
Create a
ImageDatamesh 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
Noneto 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.