Migrating to v1
What changed from Jimp v0 to v1, and how to update your code.
Jimp v1 is a rewrite in TypeScript. The goals were to:
- Make Jimp easier to use in any environment: Node.js, browsers, web workers, Bun and Cloudflare Workers.
- Make the API more consistent: every method takes either one simple value or one options object.
- Replace the
Jimp.*constants with typed named exports.
Most changes are mechanical. This page lists them, starting with a quick reference.
Quick reference
| v0 | v1 |
|---|---|
import Jimp from "jimp" |
import { Jimp } from "jimp" |
Jimp.read(path, callback) |
await Jimp.read(path) |
new Jimp(path) / new Jimp(buffer) |
await Jimp.read(path) / await Jimp.fromBuffer(buffer) |
new Jimp(w, h, color) |
new Jimp({ width: w, height: h, color }) |
image.getBufferAsync(mime) |
await image.getBuffer(mime) |
image.getBase64Async(mime) |
await image.getBase64(mime) |
image.writeAsync(path) |
await image.write(path) |
image.resize(w, h) |
image.resize({ w, h }) |
image.quality(80) |
image.write("out.jpg", { quality: 80 }) |
image.background(color) |
image.background = color |
image.getWidth(), image.getHeight() |
image.width, image.height |
image.getMIME() |
image.mime |
Jimp.MIME_PNG |
JimpMime.png |
Jimp.RESIZE_BEZIER |
ResizeStrategy.BEZIER |
Jimp.loadFont(Jimp.FONT_SANS_32_BLACK) |
loadFont(SANS_32_BLACK), from jimp and jimp/fonts |
Jimp.intToRGBA(color) |
intToRGBA(color), from jimp |
@jimp/custom, @jimp/plugins |
createJimp from @jimp/core |
Importing
jimp no longer has a default export. Import Jimp and everything else by name:
import { Jimp, JimpMime, ResizeStrategy, intToRGBA } from "jimp";@jimp/custom and @jimp/plugins are gone.
To build a Jimp with only some plugins and formats, use createJimp from @jimp/core; see Custom Jimp.
Promises instead of callbacks
In v0 there was a mix of sync and async methods for encoding images, and most async APIs took callbacks. In v1 everything that reads or encodes an image returns a promise, and callbacks aren’t supported.
The async methods have also been renamed:
// v0Jimp.read("image.png", (err, image) => { if (err) throw err; image.write("output.png");});
// v1const image = await Jimp.read("image.png");await image.write("output.png");Image methods such as resize or blur are synchronous, as they were in v0, and still change the image in place.
See Chaining and async for which calls return promises.
Creating images
The v0 constructor did everything: it read files, decoded buffers and created blank images, sometimes asynchronously. In v1 each job has its own method.
Blank images: new Jimp()
The constructor takes an options object and only creates blank images:
import { Jimp } from "jimp";
const image = new Jimp({ width: 100, height: 100 });
// With a background color, as a number or a CSS color stringconst red = new Jimp({ width: 100, height: 100, color: 0xff0000ff });const blue = new Jimp({ width: 100, height: 100, color: "#0000ff" });Files and URLs: Jimp.read
In v0 the constructor could be async. That’s an anti-pattern, so it had to go.
Use Jimp.read instead.
In Node.js it reads a file from disk, or fetches a URL:
import { Jimp } from "jimp";
const image = await Jimp.read("test/image.png");In the browser it fetches the URL:
import { Jimp } from "jimp";
const image = await Jimp.read("https://example.com/image.png");It also reads a Buffer or ArrayBuffer.
Buffers: Jimp.fromBuffer
In v0 you loaded an image from a buffer through the constructor.
In v1 use Jimp.fromBuffer:
import { Jimp } from "jimp";
const image = await Jimp.fromBuffer(buffer);Raw pixels: Jimp.fromBitmap
In v0 you passed raw pixel data, such as canvas ImageData, to the constructor.
In v1 use Jimp.fromBitmap. It’s synchronous:
import { Jimp } from "jimp";
const canvas = document.getElementById("my-canvas");const ctx = canvas.getContext("2d");
const image = Jimp.fromBitmap(ctx.getImageData(0, 0, canvas.width, canvas.height));Positional arguments to options objects
In v0 many methods took several positional arguments, which made calls hard to read and hard to extend.
In v1 a method takes either one simple value, such as blur(5), or one options object.
For example, resize used to look like this:
image.resize(100, 100);Now it looks like this:
image.resize({ w: 100, h: 100 });Jimp.AUTO is gone too: leave out w or h to keep the aspect ratio, as in image.resize({ w: 100 }).
blit
In v0, blit took the source image and coordinates as positional arguments:
image.blit(sourceImage, 10, 20, 0, 0, 100, 100);In v1, pass the source image and coordinates in an options object:
image.blit({ src: sourceImage, x: 10, y: 20, srcX: 0, srcY: 0, srcW: 100, srcH: 100,});Other methods
| v0 | v1 |
|---|---|
resize(w, Jimp.AUTO, mode) |
resize({ w, mode }) |
scaleToFit(w, h) |
scaleToFit({ w, h }) |
contain(w, h, align, mode) |
contain({ w, h, align, mode }) |
cover(w, h, align, mode) |
cover({ w, h, align, mode }) |
crop(x, y, w, h) |
crop({ x, y, w, h }) |
autocrop(tolerance) |
autocrop({ tolerance }) |
rotate(deg, mode) |
rotate(deg) or rotate({ deg, mode }) |
flip(horizontal, vertical), mirror(…) |
flip({ horizontal, vertical }) |
mask(src, x, y) |
mask({ src, x, y }), or mask(src) |
displace(map, offset) |
displace({ map, offset }) |
fisheye({ r }) |
fisheye({ radius }) |
print(font, x, y, text, maxWidth, maxHeight) |
print({ font, x, y, text, maxWidth, maxHeight }) |
dither565(), dither16() |
dither() |
grayscale() |
greyscale() |
composite still takes positional arguments, composite(src, x, y, options), but the blend modes are now the BlendMode enum; see other constants.
Encoding options
In v0, encoding options were set on the image with methods such as quality(), and it wasn’t clear which format they applied to.
In v1 you pass them when you encode, to write, getBuffer or getBase64, and they’re typed for the format you’re writing.
For example, to export a JPEG at 80% quality in v0:
import Jimp from "jimp";
const resized = await image .resize(512, Jimp.AUTO) .quality(80) .getBufferAsync(Jimp.MIME_JPEG);In v1:
import { Jimp } from "jimp";
const resized = await image .resize({ w: 512 }) .getBuffer("image/jpeg", { quality: 80 });| v0 | v1 |
|---|---|
image.quality(n) |
{ quality: n } when writing a JPEG. The default is 100. |
image.deflateLevel(n) |
{ deflateLevel: n } when writing a PNG |
image.deflateStrategy(n) |
{ deflateStrategy: n } when writing a PNG |
image.filterType(n) |
{ filterType: PNGFilterType.* } when writing a PNG |
image.rgba(false) |
{ colorType: PNGColorType.COLOR } when writing a PNG; see below |
image.background(color) |
image.background = color. Used to fill transparent areas in formats other than PNG. |
rgba
image.rgba(false) has been removed.
Since 2.0, Jimp writes an opaque image as a PNG without an alpha channel automatically.
To drop the alpha channel of an image that has transparency, pass colorType when you encode it:
import { Jimp, JimpMime, PNGColorType } from "jimp";
const image = await Jimp.read("icon.png");
await image.write("result.png", { colorType: PNGColorType.COLOR });// orconst buffer = await image.getBuffer(JimpMime.png, { colorType: PNGColorType.COLOR,});Reading and writing lists every encode option.
Removed constants
Most constants have moved to named exports.
Jimp.AUTO was only needed for positional arguments, and is gone.
Jimp.MIME_*
These have moved to the named export JimpMime:
import { JimpMime } from "jimp";
JimpMime.jpeg; // "image/jpeg"Other constants
These are now enums exported from jimp:
| v0 | v1 |
|---|---|
Jimp.RESIZE_BEZIER, Jimp.RESIZE_BILINEAR, … |
ResizeStrategy.BEZIER, … |
Jimp.HORIZONTAL_ALIGN_LEFT, _CENTER, _RIGHT |
HorizontalAlign.LEFT, .CENTER, .RIGHT |
Jimp.VERTICAL_ALIGN_TOP, _MIDDLE, _BOTTOM |
VerticalAlign.TOP, .MIDDLE, .BOTTOM |
Jimp.BLEND_SOURCE_OVER, Jimp.BLEND_DESTINATION_OVER |
BlendMode.SRC_OVER, BlendMode.DST_OVER |
Jimp.BLEND_HARDLIGHT |
BlendMode.HARD_LIGHT |
Jimp.BLEND_MULTIPLY, Jimp.BLEND_SCREEN, … |
BlendMode.MULTIPLY, BlendMode.SCREEN, … |
Jimp.EDGE_EXTEND, Jimp.EDGE_WRAP, Jimp.EDGE_CROP |
Edge.EXTEND, Edge.WRAP, Edge.CROP |
import { Jimp, Edge, ResizeStrategy, HorizontalAlign, VerticalAlign,} from "jimp";
image.resize({ w: 256, mode: ResizeStrategy.BEZIER });image.contain({ w: 256, h: 256, align: HorizontalAlign.CENTER | VerticalAlign.MIDDLE,});image.convolution({ kernel, edgeHandling: Edge.WRAP });Edge is a runtime export of jimp since 2.0. In 1.x it was exported as a type only, so Edge.WRAP was undefined at runtime; on 1.x, import it from @jimp/types instead.
Constants and enums lists every value.
Fonts
Jimp.loadFont has moved to the named export loadFont, and the Jimp.FONT_* constants have moved to named exports of jimp/fonts without the FONT_ prefix.
In v0 you would load and print with a font like this:
import Jimp from "jimp";
const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
image.print(font, 10, 10, "Hello world!");In v1:
import { Jimp, loadFont } from "jimp";import { SANS_32_BLACK } from "jimp/fonts";
const font = await loadFont(SANS_32_BLACK);
image.print({ font, x: 10, y: 10, text: "Hello world!" });For example Jimp.FONT_SANS_128_WHITE is now SANS_128_WHITE and Jimp.FONT_SANS_16_BLACK is now SANS_16_BLACK.
See Text and fonts for printing and measuring text.
Behavior changes
brightness
In v0, brightness took a number between -1 and +1, where 0 left the image unchanged.
Since v1.1.2, brightness multiplies each color channel by the value and clamps the result:
1leaves the image unchanged- greater than
1brightens it - between
0and1darkens it 0or below makes it black
For darkening, 1 + v0Value gives the same result as v0.
v0 multiplied each channel by 1 + val for negative values, which is what v1 does.
// v0image.brightness(-0.25);
// v1image.brightness(0.75);For lightening, 1 + v0Value is only an approximation.
v0 moved each channel toward white (c + (255 - c) * val), while v1 scales it, so dark pixels brighten less and black stays black.
To reproduce v0’s lightening exactly, use the tint color action with the v0 value as a percentage:
// v0image.brightness(0.25);
// v1, closeimage.brightness(1.25);
// v1, identical to v0image.color([{ apply: "tint", params: [25] }]);| v0 | v1 |
|---|---|
brightness(-1) |
brightness(0) |
brightness(-0.5) |
brightness(0.5) |
brightness(0) |
brightness(1) (no change) |
brightness(0.5) |
color([{ apply: "tint", params: [50] }]) |
brightness(1) |
color([{ apply: "tint", params: [100] }]) (all white) |
Moved and removed functions
Jimp.intToRGBAwas moved:import { intToRGBA } from "jimp";. So werergbaToInt,cssColorToHex,colorDiff,limit255,diff,distanceandcompareHashes.image.getHeight()was moved toimage.heightimage.getWidth()was moved toimage.widthimage.getMIME()was moved toimage.mimeimage.getExtension()was removed. Useimage.mime.image.getPixelColour()andimage.setPixelColour()were removed. UsegetPixelColorandsetPixelColor.Jimp.create()was removed. UseJimp.read,Jimp.fromBufferornew Jimp().
Troubleshooting
export 'methods' was not found in '@jimp/plugin-*'
If webpack reports errors like these:
export 'methods' (imported as 'circle') was not found in '@jimp/plugin-circle' (possible exports: default)export 'createJimp' (imported as 'createJimp') was not found in '@jimp/core' (possible exports: addConstants, addJimpMethods, addType, default, jimpEvChange, jimpEvMethod)then jimp v1 is being bundled with v0 copies of its own @jimp/* packages.
This happens when another dependency still uses jimp v0, for example an old library or leftover @jimp/* v0 packages in your own package.json.
Your package manager can put the v0 copies in the top-level node_modules and the v1 copies in node_modules/jimp/node_modules.
Node.js and webpack’s default settings find the v1 copies, but a bundler that only checks the top-level node_modules finds the v0 ones.
In webpack, the usual cause is a resolve.modules setting that has only an absolute path, or that lists an absolute path first:
module.exports = { resolve: { // Only finds the top-level (v0) copies modules: [path.resolve(__dirname, "node_modules")], },};To fix it, do one of the following:
- List
"node_modules"first, so nested packages are found before the top-level ones:modules: ["node_modules", path.resolve(__dirname, "node_modules")] - Remove or upgrade the dependencies that pull in v0 packages. Run
npm ls @jimp/core(oryarn why @jimp/core) to find them.
does not provide an export named 'default' or Jimp.read is not a function
You’re importing the default export, which v1 doesn’t have.
In Node.js ES modules this fails with The requested module 'jimp' does not provide an export named 'default'. With require or a bundler, it fails later with errors such as Jimp.read is not a function or Jimp is not a constructor.
Change import Jimp from "jimp" to import { Jimp } from "jimp", or const Jimp = require("jimp") to const { Jimp } = require("jimp").