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
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
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:
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:
await image.write("./out/photo.v2.JPG", { quality: 80 }); // JPEG optionsawait image.write("out.png", { quality: 80 }); // type error: PNG has no qualityawait image.write("out.gif", { quality: 80 }); // type error: GIF takes no optionsWith 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.pngbecomes 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 throwsUnsupported MIME type, for exampleUnsupported MIME type: image/webpfor.webporUnsupported MIME type: text/plainfor.txt. A directory that doesn’t exist throws Node’sENOENT: no such file or directoryerror. - Added in 2.0: plain
stringpaths, such as one frompath.join, and.icoand.jxlfiles. A path with no extension throws the error above; before, it threwUnsupported MIME type: null. - Changed in 2.0: the options are typed by the path’s extension, including
.jpg,.jpeand.tif, paths with directories, and upper-case extensions. Before,.jpgpaths weren’t matched to JPEG’s options, and.gifand.tifpaths accepted any options. - v0 difference. Jimp v0 had
writeAsync, andwritetook a callback. In v1,writereturns a promise.