Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use scipy.ndimage.rotate to rotate an image array by an angle in degrees:

from scipy import ndimage

rotated = ndimage.rotate(image, angle=45, reshape=True)

The function rotates in the plane defined by two array axes, using spline interpolation. The main choices are whether to expand the output to fit the rotated image, how to interpolate pixel values, and how to handle areas beyond the input’s edges.

Basic image rotation

For an ordinary two-dimensional image, the default axes select its two dimensions. SciPy’s reference demonstrates the call with its sample image:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from scipy import ndimage, datasets

img = datasets.ascent()
img_45 = ndimage.rotate(img, 45, reshape=False)
full_img_45 = ndimage.rotate(img, 45, reshape=True)

In the SciPy v1.18.0 documentation example, img.shape is (512, 512); the fixed-size result is (512, 512), while the expanded result is (724, 724). These are documented example values, not a performance or image-quality benchmark. See the SciPy v1.18.0 rotate API reference.

Choose whether the output can grow

Keep the original dimensions with reshape=False

This keeps the output shape the same as the input. If the rotated image extends beyond those fixed bounds, the corners or other content outside the output area are cropped.

Fit the rotated image with reshape=True

This adjusts the output dimensions to contain the rotated input. The result may be larger than the original, with additional border area around the image. How that area looks depends on the boundary mode and, for constant mode, the fill value.

Select the rotation plane for multidimensional arrays

The axes argument names the two dimensions that define the rotation plane. Its default is (1, 0), which is appropriate for a conventional two-dimensional image. For a multichannel or higher-dimensional array, specify the axes explicitly when the defaults would rotate the wrong dimensions. The function rotates within the selected plane; it does not choose a plane based on image semantics.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set interpolation and output dtype deliberately

order sets the spline interpolation order and accepts values from 0 through 5. The default is 3, cubic spline interpolation. No one order is best for every image or data type: choose based on the data and the result you need.

By default, SciPy creates an output array with the same dtype as the input. You can pass output as an array to receive the result or as a dtype to request an output type. This matters when the interpolated values should be represented in a particular type.

Prefiltering for orders above 1

With order > 1, the default prefilter=True creates a temporary float64 filtered array. Setting prefilter=False on input that has not already been spline-filtered can make the result slightly blurred. If the input is already spline-filtered, disabling prefiltering avoids filtering it again.

Control what appears beyond the image edges

The default is mode='constant' with cval=0.0. Outside samples use that constant value, and interpolation is not performed beyond the input edge. This can produce black borders for typical image data, but that fill is not necessarily suitable for every image or array. Choose the mode and fill value according to what the array represents.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Mode Boundary behavior
reflect Reflects about the edge of the last pixel. grid-mirror is a synonym.
constant Uses cval beyond the edge and does not interpolate beyond the input extent.
grid-constant Uses the constant extension and interpolates outside the input extent.
nearest Repeats the last pixel.
mirror Reflects about the center of the last pixel.
grid-wrap Wraps to the opposite edge.
wrap Wraps with overlapping endpoints, so the selected sample at the overlap is ambiguous.

For constant mode, set a different cval if zero is not an appropriate outside value. For masks, label arrays, or continuous measurements, consider how interpolation and boundary extension affect the meaning of the values rather than treating the array as ordinary color imagery.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Complete API signature

scipy.ndimage.rotate(
    input,
    angle,
    axes=(1, 0),
    reshape=True,
    output=None,
    order=3,
    mode='constant',
    cval=0.0,
    prefilter=True,
)
  • input: array-like data to rotate.
  • angle: angle in degrees.
  • axes: the two integer axes defining the rotation plane.
  • reshape: whether output dimensions adapt to contain the input.
  • output: an output array or dtype; by default the output has the input dtype.
  • order: spline interpolation order from 0 through 5.
  • mode: how samples beyond the input boundary are handled.
  • cval: constant fill value when using constant mode.
  • prefilter: whether to apply the spline prefilter.

The signature and behavior above are from the SciPy v1.18.0 API documentation; check the reference for the version you use because API details can change.

Complex arrays and alternative backends

SciPy’s documentation says complex-valued input is handled by rotating the real and imaginary components independently. It also describes experimental Python Array API Standard support. For SciPy v1.18.0, the reference lists NumPy on CPU, CuPy on GPU, PyTorch on CPU, JAX on CPU without JIT, and Dask on CPU, where the graph is computed. The listed combinations are version-sensitive; consult the versioned API reference rather than assuming all array libraries or devices are supported.

When another ndimage function may fit better

rotate is the direct choice for a fixed-angle rotation in one plane. If the task requires a custom output-to-input coordinate mapping or a broader affine operation, SciPy’s ndimage reference index also lists geometric_transform, map_coordinates, and affine_transform. Choose among them based on whether the operation is a planar rotation or a more general mapping; the index identifies these related tools but does not prescribe a particular one for every task.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.