shift#
- scipy.ndimage.shift(input,shift,output=None,order=3,mode='constant',cval=0.0,prefilter=True)[source]#
Shift an array.
The array is shifted using spline interpolation of the requested order.Points outside the boundaries of the input are filled according to thegiven mode.
- Parameters:
- inputarray_like
The input array.
- shiftfloat or sequence
The shift along the axes. If a float,
shiftis the same for eachaxis. If a sequence,shiftshould contain one value for each axis.- outputarray or dtype, optional
The array in which to place the output, or the dtype of thereturned array. By default an array of the same dtype as inputwill be created.
- orderint, optional
The order of the spline interpolation, default is 3.The order has to be in the range 0-5.
- mode{‘reflect’, ‘grid-mirror’, ‘constant’, ‘grid-constant’, ‘nearest’, ‘mirror’, ‘grid-wrap’, ‘wrap’}, optional
Themode parameter determines how the input array is extendedbeyond its boundaries. Default is ‘constant’. Behavior for each validvalue is as follows (see additional plots and details onboundary modes):
- ‘reflect’ (d c b a | a b c d | d c b a)
The input is extended by reflecting about the edge of the lastpixel. This mode is also sometimes referred to as half-samplesymmetric.
- ‘grid-mirror’
This is a synonym for ‘reflect’.
- ‘constant’ (k k k k | a b c d | k k k k)
The input is extended by filling all values beyond the edge withthe same constant value, defined by thecval parameter. Nointerpolation is performed beyond the edges of the input.
- ‘grid-constant’ (k k k k | a b c d | k k k k)
The input is extended by filling all values beyond the edge withthe same constant value, defined by thecval parameter. Interpolationoccurs for samples outside the input’s extent as well.
- ‘nearest’ (a a a a | a b c d | d d d d)
The input is extended by replicating the last pixel.
- ‘mirror’ (d c b | a b c d | c b a)
The input is extended by reflecting about the center of the lastpixel. This mode is also sometimes referred to as whole-samplesymmetric.
- ‘grid-wrap’ (a b c d | a b c d | a b c d)
The input is extended by wrapping around to the opposite edge.
- ‘wrap’ (d b c d | a b c d | b c a b)
The input is extended by wrapping around to the opposite edge, but in away such that the last point and initial point exactly overlap. In thiscase it is not well defined which sample will be chosen at the point ofoverlap.
- cvalscalar, optional
Value to fill past edges of input ifmode is ‘constant’. Defaultis 0.0.
- prefilterbool, optional
Determines if the input array is prefiltered with
spline_filterbefore interpolation. The default is True, which will create atemporaryfloat64 array of filtered values iforder>1. Ifsetting this to False, the output will be slightly blurred iforder>1, unless the input is prefiltered, i.e. it is the resultof callingspline_filteron the original input.
- Returns:
- shiftndarray
The shifted input.
See also
affine_transformAffine transformations
Notes
For complex-valuedinput, this function shifts the real and imaginarycomponents independently.
Added in version 1.6.0:Complex-valued support added.
Array API Standard Support
shifthas experimental support for Python Array API Standard compatiblebackends in addition to NumPy. Please consider testing these featuresby setting an environment variableSCIPY_ARRAY_API=1and providingCuPy, PyTorch, JAX, or Dask arrays as array arguments. The followingcombinations of backend and device (or other capability) are supported.Library
CPU
GPU
NumPy
✅
n/a
CuPy
n/a
✅
PyTorch
✅
⛔
JAX
⚠️ no JIT
⛔
Dask
⚠️ computes graph
n/a
SeeSupport for the array API standard for more information.
Examples
Import the necessary modules and an exemplary image.
>>>fromscipy.ndimageimportshift>>>importmatplotlib.pyplotasplt>>>fromscipyimportdatasets>>>image=datasets.ascent()
Shift the image vertically by 20 pixels.
>>>image_shifted_vertically=shift(image,(20,0))
Shift the image vertically by -200 pixels and horizontally by 100 pixels.
>>>image_shifted_both_directions=shift(image,(-200,100))
Plot the original and the shifted images.
>>>fig,axes=plt.subplots(3,1,figsize=(4,12))>>>plt.gray()# show the filtered result in grayscale>>>top,middle,bottom=axes>>>foraxinaxes:...ax.set_axis_off()# remove coordinate system>>>top.imshow(image)>>>top.set_title("Original image")>>>middle.imshow(image_shifted_vertically)>>>middle.set_title("Vertically shifted image")>>>bottom.imshow(image_shifted_both_directions)>>>bottom.set_title("Image shifted in both directions")>>>fig.tight_layout()
