DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk3 min

How to Rotate an Image Array with SciPy and Tune Its Output

Use scipy.ndimage.rotate to rotate an image array by degrees, while controlling output dimensions, axes, interpolation, and edge fill.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use scipy.ndimage.rotate to rotate an image array by an angle in degrees. A basic call is ndimage.rotate(image, angle=45, reshape=True); choose the output shape, rotation axes, interpolation order, and edge handling to suit your data.

Basic rotation with SciPy

Import ndimage and pass it an image array and the angle in degrees:

As an Amazon Associate I earn from qualifying purchases.

from scipy import ndimage

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

The function rotates the array in the plane defined by axes, using spline interpolation of the requested order. The current SciPy v1.18.0 API documents this function and its defaults in the official rotate reference.

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

Choose whether to keep the original dimensions

With reshape=True (the default), SciPy adjusts the output dimensions to contain the rotated input. With reshape=False, the output retains the input shape, so rotated corners that fall outside those bounds can be cropped.

from scipy import ndimage, datasets

img = datasets.ascent()
fixed = ndimage.rotate(img, 45, reshape=False)
expanded = ndimage.rotate(img, 45, reshape=True)

In SciPy’s documented example, the input shape is (512, 512); the fixed-shape result is (512, 512), while the expanded result is (724, 724). These are the reference example’s values. Expanded output may have fill around the rotated image, depending on the boundary mode.

Set the rotation plane with axes

axes is a pair of integer array axes that defines the plane of rotation. Its default is (1, 0), which selects both dimensions of a conventional two-dimensional image. For an array with more dimensions—such as a multichannel image or a volume—specify the pair explicitly so the intended dimensions rotate.

# Example only: choose the two axes that represent the image plane
rotated = ndimage.rotate(image, 30, axes=(1, 0))

The correct axis pair depends on how the array is laid out; do not assume that a rotation will treat a channel dimension as channels rather than as a spatial axis.

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

Choose interpolation order for the data

The order parameter selects spline interpolation order from 0 through 5. The default is order=3, cubic spline interpolation. A different order changes how values between input samples are calculated; no single order is best for every image or measurement.

  • Use the default when cubic spline interpolation is appropriate for your task.
  • Consider order 0 when you need to avoid creating interpolated intermediate values, as can matter for masks or label arrays.
  • For continuous-valued image data, choose an order based on the desired interpolation behavior and inspect the result for your application.

For order > 1, the default prefilter=True applies spline prefiltering and creates a temporary float64 array. Setting prefilter=False on data that has not already been spline-filtered can slightly blur the result; it is useful when the input has already been spline-filtered and you want to avoid filtering it again.

Control what appears beyond the image edges

The default is mode='constant' with cval=0.0. Samples outside the input boundary use that constant, and interpolation is not performed beyond the input edge. For ordinary imagery this can create a zero-valued border; for masks, labels, or measurements, choose edge handling and fill values deliberately.

Mode Boundary behavior
reflect / grid-mirror Reflects about the edge of the last pixel; grid-mirror is a synonym for reflect.
constant Uses cval outside the input and does not interpolate beyond the input edge.
grid-constant Uses constant extension and interpolates outside the input extent.
nearest Repeats the last pixel beyond the boundary.
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 example, set a nonzero constant fill explicitly when that is the intended border behavior:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
rotated = ndimage.rotate(image, 30, reshape=True, mode="constant", cval=255)

The value 255 is only an example; choose a value appropriate to the array’s data and dtype.

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

Other parameters and data types

The full SciPy v1.18.0 signature is scipy.ndimage.rotate(input, angle, axes=(1, 0), reshape=True, output=None, order=3, mode='constant', cval=0.0, prefilter=True). input is array-like. You can provide output as an array to receive the result or as a dtype; otherwise, SciPy creates an output array with the input’s dtype.

Complex-valued inputs are handled by rotating their real and imaginary components independently. The same API page describes experimental Python Array API Standard support for selected backends and device combinations, including NumPy on CPU, CuPy on GPU, PyTorch on CPU, JAX on CPU without JIT, and Dask on CPU (which computes the graph). This support is experimental and version-sensitive; check the version-specific API reference before relying on a backend.

When rotate is not the right operation

ndimage.rotate is the direct choice for a fixed-angle rotation within one plane. If you need a custom output-to-input coordinate mapping or a more general affine operation, SciPy’s ndimage reference index also lists geometric_transform, map_coordinates, and affine_transform. Consult the individual function references for their specific behavior.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.