Jimp getBase64: image to data URI
Jimp can read an image, process it, and export a Base64 data URI. In the official core implementation checked on October 8, 2026, getBase64() returns a Promise, so use await. The resolved value already contains the header, such as data:image/png;base64,....
For a one-off conversion without installing a library, use the Image to Base64 tool. Jimp is useful when your Node.js workflow also needs to resize or re-encode an image before producing the text output.
What getBase64 returns
await image.getBase64("image/png") returns a complete PNG data URI. That string can be assigned to a browser <img src> or used where the destination explicitly accepts data URIs.
An API field documented as “Base64 content” may instead expect only the text after the comma. Check the destination's contract before sending it. The API output-shape guide covers raw Base64 versus a full data URI.
Complete Node.js example
Install Jimp in your own project if it is not already a dependency, then save this as jimp-data-uri.mjs. Run it with node jimp-data-uri.mjs input.png. The script writes a data URI and a raw Base64 file so you can choose the output your destination needs.
import { Jimp } from "jimp";
import { writeFile } from "node:fs/promises";
const input = process.argv[2];
if (!input) throw new Error("Usage: node jimp-data-uri.mjs input.png");
const image = await Jimp.read(input);
// Optional processing before export:
// image.resize({ w: 320 });
const dataUri = await image.getBase64("image/png");
const prefix = "data:image/png;base64,";
if (!dataUri.startsWith(prefix)) throw new Error("Unexpected PNG data URI");
const rawBase64 = dataUri.slice(prefix.length);
await writeFile("image.data-uri.txt", dataUri, "utf8");
await writeFile("image.base64.txt", rawBase64, "utf8");
console.log(`Wrote ${dataUri.length} data-URI characters.`);
This example exports PNG even if the input is a supported JPEG. Use image/jpeg and the options supported by your installed Jimp version when you want JPEG output. PNG and JPEG differ in transparency and compression; a Base64 string does not erase those differences. See image resizing before Base64 when payload size matters.
Read a data URI back into Jimp
For a trusted data URI produced by the previous script, remove the prefix and decode its Base64 payload into a Buffer. Save the following as jimp-read-data-uri.mjs:
import { Jimp } from "jimp";
import { readFile } from "node:fs/promises";
const dataUri = (await readFile("image.data-uri.txt", "utf8")).trim();
const match = /^data:image\/(?:png|jpeg);base64,([A-Za-z0-9+/]+={0,2})$/.exec(dataUri);
if (!match) throw new Error("Expected a PNG or JPEG Base64 data URI");
const bytes = Buffer.from(match[1], "base64");
if (!bytes.length || bytes.toString("base64") !== match[1]) {
throw new Error("Invalid Base64 payload");
}
const image = await Jimp.fromBuffer(bytes);
console.log(image.bitmap.width, image.bitmap.height);
This is a local-file example. For untrusted uploads, also enforce encoded-byte and decoded-pixel limits before processing. A compressed file can be small while its decoded bitmap is large.
Preserve the original bytes when you do not need editing
Jimp's getBase64() calls its image encoder. It does not promise byte-for-byte preservation of the source file or its metadata. If you only need Base64 of the original bytes, Node's standard library is enough:
import { readFile } from "node:fs/promises";
const rawBase64 = (await readFile("input.png")).toString("base64");
const dataUri = `data:image/png;base64,${rawBase64}`;
console.log(dataUri);
Use the MIME type that actually matches the input. For browser-side byte encoding, how to convert an image to Base64 shows the FileReader path.
Troubleshooting and version differences
I see [object Promise]. Await getBase64(). Do not pass its Promise directly to an <img> or JSON payload.
My API rejects the string. Check whether it expects raw Base64 rather than a data URI. Do not encode an already encoded Base64 string again.
An old example uses a callback. Match the example to your installed Jimp version. Current named imports and Promise methods should not be combined with an older callback tutorial.
The output file differs from the input. That is expected when Jimp decodes and re-encodes the image. Use readFile().toString("base64") when exact original-byte transport is the requirement.
Related guides
Continue with API payload formats, resizing an image before encoding, or the browser conversion walkthrough.