stable-diffusion-client
Version:
smart stable-diffusion-webui client
17 lines (16 loc) • 587 B
JavaScript
/**
* Converts an image buffer to base64
*
* @param {Buffer} image image buffer to convert to base64
* @param {boolean} raw if true, returns the raw base64 string, if false, returns a data url with the base64 string
* @returns {Promise<string>} base64 encoded image
*/
export async function toBase64(image, raw = false) {
if (raw) {
const buffer = await image.raw().toBuffer();
return buffer.toString("base64");
}
const header = "data:image/png;base64,";
const buffer = await image.png().toBuffer();
return header + buffer.toString("base64");
}