Rotate a 2D image while choosing whether to preserve or expand its canvas.
Image processing
SciPy · Function
Rotates an array in the plane defined by two axes using spline interpolation. The angle is given in degrees, with controls for output shape, interpolation, boundary behavior, and destination storage.
rotate applies spline interpolation to rotate an array within the plane defined by two selected axes. The angle is specified in degrees; optional settings control output shape, interpolation order, boundary handling, fill value, and prefiltering.
scipy.ndimage.rotate(input, angle, axes=(1, 0), reshape=True, output=None, order=3, mode='constant', cval=0.0, prefilter=True)| Name | Type | Description |
|---|---|---|
input | array_like | Input array. |
angle | float | Rotation angle in degrees. |
axes | tuple of 2 ints, optional | Two axis indices defining the rotation plane; defaults to the first two axes. |
reshape | bool, optional | Whether to adapt the output shape so the complete input fits; default is True. |
output | array or dtype, optional | Destination array or dtype of the returned array; by default a same-dtype array is created. |
order | int, optional | Spline interpolation order, from 0 through 5; default is 3. |
mode | {'reflect', 'grid-mirror', 'constant', 'grid-constant', 'nearest', 'mirror', 'grid-wrap', 'wrap'}, optional | Boundary mode used when sampling outside the input. |
cval | float, optional | Value used beyond the input boundary when the mode requires a constant value; default is 0.0. |
prefilter | bool, optional | Whether to prefilter the input for spline interpolation; default is True. |
ndarray containing the rotated array; if output is supplied as an array, no separate array is returned.
ndimage.rotate to rotate an array.axes select the plane in which rotation occurs.order.angle.axes accepts a pair of axis indices and defaults to the first two axes.output supports either a destination array or a dtype for the returned result.order between 0 and 5; values outside that range are invalid.reshape=True, the result can have a different shape; use False when a fixed shape is required.output specifies a destination array or dtype, the returned array uses the input dtype.import numpy as np
from scipy import ndimage
image = np.arange(16).reshape(4, 4)
rotated = ndimage.rotate(image, 45, reshape=False, order=0)
print(rotated.shape)
Rotates a small 2D array by 45 degrees around its first two axes while retaining its original shape.
Image processing
Multidimensional array analysis
Preallocated numerical workflows
scipy.ndimage.rotate do, and which axes does it use by default?It rotates the array in the plane described by axes, using spline interpolation with the requested order. The angle is measured in degrees, and the default axes are the first two axes.
Follow-up: How do you avoid changing the result dimensions? Set reshape=False; otherwise the output shape is adjusted to contain the rotated input completely.
order?order selects the spline interpolation order and defaults to 3. The documented valid range is 0 through 5.
Follow-up: For nearest-neighbor-like behavior, choose order 0; for smoother interpolation, choose a higher supported order while considering the data type and application.
output argument affect allocation and return behavior?output can be an existing array receiving the result or a dtype used for the returned array. If an output array is supplied, the function does not return a separate result array.
Follow-up: When no destination is supplied, what determines the output dtype? By default, it is the same dtype as the input array.
scipy.ndimage.affine_transformscipy.ndimage.zoomscipy.ndimage.shift