Skip to content
docs
GitHub 4.5M /week v2.2.0
Size & shape

rotate()

Rotate the image counter-clockwise by any angle.

From @jimp/plugin-rotate

rotate turns the image counter-clockwise about its center by a number of degrees. Use a negative angle to turn it clockwise. By default the canvas grows so the whole rotated image fits.

Signature

rotate.d.ts
image.rotate(deg: number): this
image.rotate(options: { deg: number; mode?: boolean | ResizeStrategy }): this

rotate changes the image in place and returns it, so you can chain more calls after it.

Options

Pass the angle on its own, or an object to also set mode.

Option Type Default Description
deg number required The angle in degrees. Positive values turn counter-clockwise, negative values clockwise.
mode boolean | ResizeStrategy true true grows the canvas to fit the rotated image. false keeps the original size and clips the corners. A ResizeStrategy grows the canvas like true and smooths the result. See Smooth edges.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.jpg");
// Turn a landscape photo into a portrait one: 800 x 600 becomes 600 x 800
image.rotate(90);
await image.write("photo-rotated.jpg");

Rotate clockwise

example.ts
image.rotate(-90);

Keep the size

example.ts
image.rotate({ deg: 15, mode: false });

The image stays the same size and is rotated about its center. The rotated corners are cut off, and the areas the image no longer covers are filled with image.background.

Smooth edges

With true or false, each output pixel copies the nearest source pixel, so straight edges at angles like 30° look stepped. Pass a ResizeStrategy as mode to interpolate the pixels instead. The canvas grows as it does with true:

example.ts
import { Jimp, ResizeStrategy } from "jimp";
const image = await Jimp.read("photo.jpg");
image.rotate({ deg: 30, mode: ResizeStrategy.BILINEAR });

BILINEAR is the fastest of these. BICUBIC, MITCHELL and LANCZOS keep more detail but take a few times longer, and every strategy is slower than the nearest pixel. NEAREST_NEIGHBOR gives the same result as true.

Fill the corners with a color

New pixels use image.background, which is transparent black (0x00000000) for images loaded with Jimp.read. Set it before rotating to fill them with a color, which matters for formats without transparency such as JPEG:

example.ts
image.background = 0xffffffff; // opaque white, as 0xRRGGBBAA
image.rotate(30);
await image.write("photo-tilted.jpg");

Canvas size

With mode: true (the default) or a ResizeStrategy, angles that aren’t a multiple of 90 make the canvas larger, so nothing is cut off. Each side is the side of the rotated image’s bounding box, rounded up to whole pixels:

width = ceil(|w · cos θ| + |h · sin θ|)
height = ceil(|w · sin θ| + |h · cos θ|)

If needed, a side is made one pixel larger so it’s even when the image side along it is even, and odd when that is odd. That keeps the rotated image centered on whole pixels. For example, an 800 × 600 image rotated by 45° becomes 990 × 990, and by 30° becomes 994 × 920. Multiples of 90 swap or keep the sides exactly: rotate(90) on 800 × 600 gives 600 × 800, and rotate(180) keeps 800 × 600.

To get back to the original size after a rotation, follow it with cover or crop.

Try it

Drag deg to rotate, and switch mode to compare growing the canvas with keeping its size, or pick a ResizeStrategy to smooth the edges.

Try rotate()
image.rotate(45)

Rotate counter-clockwise by any angle. true grows the canvas to fit, false keeps its size, and a ResizeStrategy grows it and smooths the edges.

45°
Loading Jimp…Drop an image to load it
PNG
index.js
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Change an option to run it await image.write("output.png");

Notes

  • Right angles are exact. Multiples of 90 move pixels without resampling, even when mode is a ResizeStrategy, so rotate(90) four times gives back the original. This is used whenever the canvas may change size, or the image is square, or the angle is 180.
  • mode: false with 90°. On a non-square image, rotate({ deg: 90, mode: false }) keeps the original width and height, so the ends of the rotated image are cut off evenly on both sides.
  • Transparency. With a ResizeStrategy, colors are weighted by alpha, so edges against a transparent background don’t get a dark fringe. Pixels outside the image count as image.background, which antialiases the edges against it. With a growing canvas, the image is drawn over image.background first, so partly transparent pixels blend with it.
  • Angles wrap. deg is taken modulo 360, so rotate(450) is the same as rotate(90), and rotate(360) does nothing.
  • Validation. deg must be a number. A string such as "90" fails validation with Invalid input.
  • Other transforms. To skew, or to combine rotating with scaling or flipping in one step, use skew or affine. rotate(deg) is the same as affine with the matrix [cos θ, -sin θ, sin θ, cos θ, 0, 0].
  • Changed in 2.0: rotations that aren’t a multiple of 90 are centered. Before, the rotated image came out one pixel up and to the left, so mode: false cut off an edge, and the canvas was the bounding box plus one pixel, rounded up to an even number.
  • Changed in 2.0: a ResizeStrategy as mode interpolates the pixels. Before, it behaved exactly like true.
  • Changed in 2.0: with mode: false, 90° and 270° rotations of a non-square image are centered too. Before, they were off by a pixel.
  • Added in 2.0: the LANCZOS, LANCZOS2 and MITCHELL strategies for mode.