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
image.rotate(deg: number): thisimage.rotate(options: { deg: number; mode?: boolean | ResizeStrategy }): thisrotate 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
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 800image.rotate(90);
await image.write("photo-rotated.jpg");Rotate clockwise
image.rotate(-90);Keep the size
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:
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:
image.background = 0xffffffff; // opaque white, as 0xRRGGBBAAimage.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.
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.
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
modeis aResizeStrategy, sorotate(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: falsewith 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 asimage.background, which antialiases the edges against it. With a growing canvas, the image is drawn overimage.backgroundfirst, so partly transparent pixels blend with it. - Angles wrap.
degis taken modulo 360, sorotate(450)is the same asrotate(90), androtate(360)does nothing. - Validation.
degmust be a number. A string such as"90"fails validation withInvalid input. - Other transforms. To skew, or to combine rotating with scaling or flipping in one step, use
skeworaffine.rotate(deg)is the same asaffinewith 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: falsecut off an edge, and the canvas was the bounding box plus one pixel, rounded up to an even number. - Changed in 2.0: a
ResizeStrategyasmodeinterpolates the pixels. Before, it behaved exactly liketrue. - 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,LANCZOS2andMITCHELLstrategies formode.