Skip to content
docs
GitHub 4.5M /week v2.2.0
Loading & saving

write()

Encode the image and save it to a file. The extension picks the format.

From @jimp/core

write encodes the image and saves it to a file in Node. The file extension decides the format, so "out.png" writes a PNG and "out.jpg" writes a JPEG.

Signature

write.d.ts
image.write(path: string, options?: EncodeOptions): Promise<void>

write returns a promise that resolves once the file is written. It doesn’t change the image.

Parameters

Parameter Type Default Description
path string required Where to save the file, relative to the current working directory. Its extension picks the format.
options object none Encoder options for that format, the same as for getBuffer, for example { quality: 80 } for JPEG.

Example

example.ts
import { Jimp } from "jimp";
const image = await Jimp.read("photo.png");
image.resize({ w: 1200 });
await image.write("photo-large.png");
await image.write("photo-large.jpg", { quality: 85 });

Paths built at runtime

write accepts any string, including paths from path.join or user input:

example.ts
import path from "node:path";
const name = "photo-small";
await image.write(path.join("output", `${name}.png`));

The directory must already exist; write doesn’t create it.

Options are typed by extension

With a literal path, TypeScript checks the options against the format of its last extension, ignoring case. That works with directories and dots in the path too, so "./out/photo.v2.JPG" takes JPEG options:

example.ts
await image.write("./out/photo.v2.JPG", { quality: 80 }); // JPEG options
await image.write("out.png", { quality: 80 }); // type error: PNG has no quality
await image.write("out.gif", { quality: 80 }); // type error: GIF takes no options

With a string variable, or an extension no format handles, write accepts the options of any of your formats, and rejects keys that none of them take.

Extensions and formats

The extension is looked up with the mime package and isn’t case sensitive. With the default jimp package:

Extensions Format Options
.png PNG PNG options
.jpg, .jpeg, .jpe JPEG quality, exif, icc
.gif GIF none
.bmp BMP bitPP and header options
.tif, .tiff TIFF none
.ico ICO sizes

.webp, .avif, .jxl and .jp2 work once you add their WASM format plugins. Some formats have no extension write can map, such as JPEG 2000 codestreams (.j2c, .j2k); encode those with getBuffer and write the bytes yourself.

Notes

  • The original format isn’t kept automatically. An image read from a JPEG and written to out.png becomes a PNG. Jimp goes by the extension only.
  • Writing replaces existing files without asking.
  • One image per file. To write an animated GIF or a multi-page TIFF, use Jimp.writeFrames.
  • Errors. A path with no extension throws Cannot write "<path>": the path has no recognisable file extension to determine the image format. An extension whose format isn’t available throws Unsupported MIME type, for example Unsupported MIME type: image/webp for .webp or Unsupported MIME type: text/plain for .txt. A directory that doesn’t exist throws Node’s ENOENT: no such file or directory error.
  • Added in 2.0: plain string paths, such as one from path.join, and .ico and .jxl files. A path with no extension throws the error above; before, it threw Unsupported MIME type: null.
  • Changed in 2.0: the options are typed by the path’s extension, including .jpg, .jpe and .tif, paths with directories, and upper-case extensions. Before, .jpg paths weren’t matched to JPEG’s options, and .gif and .tif paths accepted any options.
  • v0 difference. Jimp v0 had writeAsync, and write took a callback. In v1, write returns a promise.