Skip to content
docs
GitHub 4.5M /week v2.2.0
Concept

Constants and enums

The enums and constants exported by jimp: resize strategies, blend modes, alignment, edge handling, color actions, MIME types and format options.

jimp exports a handful of enums and constants for options that take one of a fixed set of values. Import them by name alongside Jimp; they replace the Jimp.SOMETHING constants of v0 (see Migrate to v1).

example.ts
import {
Jimp,
ResizeStrategy,
BlendMode,
HorizontalAlign,
VerticalAlign,
JimpMime,
PNGColorType,
PNGFilterType,
BmpCompression,
Edge,
ColorActionName,
defaultPlugins,
defaultFormats,
} from "jimp";
Export Used by
ResizeStrategy mode of resize, scale, scaleToFit, contain, cover, rotate, affine, skew
BlendMode mode of composite
HorizontalAlign, VerticalAlign align of contain and cover; alignmentX and alignmentY of print
JimpMime getBuffer, getBase64
PNGColorType colorType when writing PNG
PNGFilterType filterType when writing PNG
BmpCompression Not used by any Jimp option
Edge getPixelIndex, edgeHandling of convolution
ColorActionName apply of the color actions
defaultPlugins, defaultFormats createJimp from @jimp/core

Every one of them is a value you can use at run time, in TypeScript and plain JavaScript alike. Most are TypeScript enums, so you can also pass the underlying value (for example "bilinearInterpolation" instead of ResizeStrategy.BILINEAR). Using the names is clearer and lets TypeScript check them.

ResizeStrategy

The resampling algorithm for methods that change an image’s size. Pass it as the mode option of resize, scale, scaleToFit, contain and cover. Without a mode, these methods use Jimp’s default resampler. rotate, affine and skew also take it as mode, to interpolate the pixels they move instead of taking the nearest one.

Member Value Meaning
NEAREST_NEIGHBOR "nearestNeighbor" Copies the nearest source pixel. Fastest, and keeps hard edges, so it suits pixel art; photos look blocky.
BILINEAR "bilinearInterpolation" Weighted average of the 4 nearest pixels. Smoother than nearest neighbor.
BICUBIC "bicubicInterpolation" Uses the 16 nearest pixels. Smoother and sharper than bilinear, and slower. A good choice for photos.
HERMITE "hermiteInterpolation" Hermite interpolation. Similar quality to bicubic with less blurring.
BEZIER "bezierInterpolation" Bézier interpolation.
LANCZOS "lanczosInterpolation" Lanczos with a 3-pixel window, as in Pillow and ImageMagick. Keeps the most detail when enlarging or shrinking; the slowest, with slight ringing next to hard edges.
LANCZOS2 "lanczos2Interpolation" Lanczos with a 2-pixel window. A little softer than LANCZOS, with less ringing, and faster.
MITCHELL "mitchellInterpolation" The Mitchell–Netravali cubic filter. A balance of sharpness and smoothness with almost no ringing; faster than LANCZOS.
example.ts
import { Jimp, ResizeStrategy } from "jimp";
const image = await Jimp.read("photo.jpeg");
image.resize({ w: 200, mode: ResizeStrategy.LANCZOS });

Added in 2.0: LANCZOS, LANCZOS2 and MITCHELL.

Try each one: pick a strategy from the mode list and change the width. The difference is easiest to see with NEAREST_NEIGHBOR.

Try resize()
image.resize({ w: 180 })

Resize to a width and/or height. With only one side set, the other keeps the aspect ratio.

180px
keep ratio
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");

BlendMode

How composite combines each source pixel with the pixel under it. Pass it as the mode option. The default is BlendMode.SRC_OVER. Compositing shows each mode side by side.

Member Value Result
SRC_OVER "srcOver" Draws the source on top, with its transparency. The default.
DST_OVER "dstOver" Draws the source underneath the destination.
MULTIPLY "multiply" Multiplies the colors. Never lighter; white leaves the destination unchanged.
ADD "add" Adds the colors. Brightens.
SCREEN "screen" Inverse of multiply. Never darker; black leaves the destination unchanged.
OVERLAY "overlay" Multiply in dark areas of the destination, screen in light ones. Increases contrast.
DARKEN "darken" Keeps the darker value of each channel.
LIGHTEN "lighten" Keeps the lighter value of each channel.
HARD_LIGHT "hardLight" Like overlay, but decided by the source: bright source colors screen, dark ones multiply.
DIFFERENCE "difference" Subtracts the smaller value from the larger. White inverts, black has no effect.
EXCLUSION "exclusion" Like difference, with lower contrast.
COLOR_DODGE "colorDodge" Brightens the destination to reflect the source. Black has no effect.
COLOR_BURN "colorBurn" Darkens the destination to reflect the source. White has no effect.
SOFT_LIGHT "softLight" A softer hard light: light source colors dodge, dark ones burn.
HUE "hue" The source’s hue, with the destination’s saturation and luminosity.
SATURATION "saturation" The source’s saturation, with the destination’s hue and luminosity.
COLOR "color" The source’s hue and saturation, with the destination’s luminosity. Tints.
LUMINOSITY "luminosity" The source’s luminosity, with the destination’s hue and saturation.
example.ts
import { Jimp, BlendMode } from "jimp";
const base = await Jimp.read("photo.jpeg");
const texture = await Jimp.read("paper.png");
base.composite(texture, 0, 0, { mode: BlendMode.MULTIPLY, opacitySource: 0.5 });

HorizontalAlign and VerticalAlign

Where to place content inside a larger area. The values are bit flags, so a horizontal and a vertical value can be combined with | into one number.

Member Value
HorizontalAlign.LEFT 1
HorizontalAlign.CENTER 2
HorizontalAlign.RIGHT 4
VerticalAlign.TOP 8
VerticalAlign.MIDDLE 16
VerticalAlign.BOTTOM 32

contain and cover take one combined align value. The default is HorizontalAlign.CENTER | VerticalAlign.MIDDLE.

example.ts
import { Jimp, HorizontalAlign, VerticalAlign } from "jimp";
const image = await Jimp.read("photo.jpeg");
image.cover({ w: 300, h: 300, align: HorizontalAlign.LEFT | VerticalAlign.TOP });

print takes them separately, as alignmentX and alignmentY of the text object. The defaults are LEFT and TOP. The text is aligned in a box that starts at x, y and is maxWidth by maxHeight; without them, the box reaches the right and bottom edges of the image. See Text and fonts.

example.ts
image.print({
font,
x: 0,
y: 0,
text: {
text: "Hello",
alignmentX: HorizontalAlign.CENTER,
alignmentY: VerticalAlign.MIDDLE,
},
maxWidth: image.width,
maxHeight: image.height,
});

Each direction takes at most one flag. A direction you leave out is centered, so HorizontalAlign.RIGHT alone means right and middle. Two flags for the same direction throw align must combine at most one HorizontalAlign and one VerticalAlign flag. contain and cover also accept align: { x, y } with fractions from 0 (left or top) to 1 (right or bottom), like CSS object-position.

JimpMime

An object with the MIME type of each default format. Pass its values to getBuffer and getBase64 instead of typing the strings.

Key Value
bmp "image/bmp"
gif "image/gif"
ico "image/x-icon"
jpeg "image/jpeg"
png "image/png"
tiff "image/tiff"
example.ts
import { Jimp, JimpMime } from "jimp";
const image = await Jimp.read("photo.png");
const buffer = await image.getBuffer(JimpMime.jpeg, { quality: 80 });

The default formats also include "image/x-ms-bmp" and "image/vnd.microsoft.icon", which aren’t in JimpMime. Added in 2.0: JimpMime.ico. Formats added with plugins, such as WEBP, aren’t either; use their MIME string directly. See Formats.

PNGColorType

The PNG color type to write, passed as the colorType option when you write a PNG. By default Jimp picks the smallest type that stores the image without loss: it drops the alpha channel when every pixel is opaque, and the color channels when every pixel is grey.

Member Value Stores
GRAYSCALE 0 Grey only
COLOR 2 Red, green, blue
GRAYSCALE_ALPHA 4 Grey and alpha
COLOR_ALPHA 6 Red, green, blue and alpha
example.ts
import { Jimp, PNGColorType } from "jimp";
const image = await Jimp.read("photo.png");
// Always write RGBA, even for opaque or grey images
await image.write("output.png", { colorType: PNGColorType.COLOR_ALPHA });

Forcing a type without alpha discards transparency, and a grey type discards color.

PNGFilterType

The filter PNG applies to each row before compressing it, passed as the filterType option. Filters don’t change the image, only how well it compresses. The default, AUTO, tries every filter on each row and keeps the one that’s likely to compress best, which is usually the right choice.

Member Value Filter
AUTO -1 Choose the best filter for each row. The default.
NONE 0 No filter.
SUB 1 Difference from the pixel to the left.
UP 2 Difference from the pixel above.
AVERAGE 3 Difference from the average of the left and upper pixels.
PATH 4 The Paeth filter: difference from whichever of left, above or upper-left is closest to a prediction.
example.ts
import { Jimp, JimpMime, PNGFilterType } from "jimp";
const image = await Jimp.read("photo.png");
// Faster to encode, usually larger
const buffer = await image.getBuffer(JimpMime.png, { filterType: PNGFilterType.NONE });

BmpCompression

The compression methods of the BMP format, re-exported from the bmp-ts library. No Jimp option takes this enum: Jimp always writes uncompressed BMP files. It’s exported for code that works with BMP headers directly.

Member Value
NONE 0
BI_RLE8 1
BI_RLE4 2
BI_BIT_FIELDS 3
BI_ALPHA_BIT_FIELDS 6

Edge

How getPixelIndex and the edgeHandling option of convolution treat coordinates outside the image.

Member Value Outside coordinates
EXTEND 1 Clamp to the nearest edge pixel. The default.
WRAP 2 Wrap around to the opposite side.
CROP 3 Treat as missing. getPixelIndex returns -1.
example.ts
import { Jimp, Edge } from "jimp";
const image = await Jimp.read("photo.png");
image.convolution({
kernel: [
[0, -1, 0],
[-1, 5, -1],
[0, -1, 0],
],
edgeHandling: Edge.WRAP,
});

Edge replaces v0’s Jimp.EDGE_EXTEND, Jimp.EDGE_WRAP and Jimp.EDGE_CROP. Changed in 2.0: jimp exports Edge as a value; before, it was a type-only export, so Edge.WRAP was undefined at run time. See Colors and pixels.

ColorActionName

The names of the actions of color, for its apply field. Each member is the action name as a string, so ColorActionName.HUE is "hue", and plain strings work just as well.

Member Value
HUE "hue"
SPIN "spin"
SATURATE "saturate"
DESATURATE "desaturate"
GREYSCALE "greyscale"
LIGHTEN "lighten"
DARKEN "darken"
BRIGHTEN "brighten"
TINT "tint"
SHADE "shade"
MIX "mix"
MULTIPLY "multiply"
XOR "xor"
RED "red"
GREEN "green"
BLUE "blue"
example.ts
import { Jimp, ColorActionName } from "jimp";
const image = await Jimp.read("photo.png");
image.color([
{ apply: ColorActionName.HUE, params: [30] },
{ apply: ColorActionName.MULTIPLY, params: [1.1, 1, 0.9] },
]);

Changed in 2.0: ColorActionName is exported from jimp, and has a MULTIPLY member for the new multiply action. Before, it was only exported by @jimp/plugin-color.

defaultPlugins and defaultFormats

The building blocks of the Jimp class. jimp creates it with createJimp({ plugins: defaultPlugins, formats: defaultFormats }), and you can do the same to add your own plugins or formats on top.

  • defaultPlugins is an array of the method objects of the 19 built-in plugins: blit, blur, border, circle, color, contain, cover, crop, displace, dither, fisheye, flip, hash, mask, print, resize, rotate, threshold and quantize.
  • defaultFormats is an array of format factories, one each for image/bmp, image/x-ms-bmp, image/gif, image/x-icon, image/vnd.microsoft.icon, image/jpeg, image/png and image/tiff.
example.ts
import { createJimp } from "@jimp/core";
import { defaultFormats, defaultPlugins } from "jimp";
import webp from "@jimp/wasm-webp";
const Jimp = createJimp({
plugins: defaultPlugins,
formats: [...defaultFormats, webp],
});

See Custom Jimp and WEBP / WASM.

See also