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

flip()

Mirror the image horizontally, vertically or both.

From @jimp/plugin-flip

flip mirrors the image. A horizontal flip swaps left and right, like a mirror; a vertical flip turns it upside down.

Signature

flip.d.ts
image.flip(options: { horizontal?: boolean; vertical?: boolean }): this

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

Options

Option Type Default Description
horizontal boolean false Mirror left to right.
vertical boolean false Mirror top to bottom.

The options object itself is required, even though both options are optional.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("selfie.jpg");
// Undo the mirroring of a front camera
image.flip({ horizontal: true });
await image.write("selfie-fixed.jpg");

Flip both ways

Flipping in both directions gives the same result as rotating by 180°:

example.ts
image.flip({ horizontal: true, vertical: true });
// same pixels as image.rotate(180)

Try it

Toggle horizontal and vertical.

Try flip()
image.flip({ horizontal: true, vertical: false })

Mirror the image horizontally, vertically or both.

horizontal
vertical
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

  • The size doesn’t change. Every pixel is moved, none are resampled, so flipping twice restores the original exactly.
  • No options does nothing. flip({}) leaves the image as it is. Calling flip() with no argument at all fails validation with Invalid input: expected object, received undefined.
  • v0 differences. v0 took two booleans: flip(horizontal, vertical). In v1 pass an object, and the mirror alias is gone.