Skip to content

Instantly share code, notes, and snippets.

@dooderstem
Last active July 10, 2023 06:07
Show Gist options
  • Save dooderstem/907d6c5a8e839f5ccd3468e275cb4b95 to your computer and use it in GitHub Desktop.
Save dooderstem/907d6c5a8e839f5ccd3468e275cb4b95 to your computer and use it in GitHub Desktop.
The "JavaScript Image Manipulation Program" :-) #JIMP #Node #JavaScript
// npm install --save jimp
var Jimp = require("jimp");
// Example usage (Promise will never resolve if callback is passed):
// open a file called "lenna.png"
Jimp.read("lenna.png", (err, lenna) => {
if (err) throw err;
lenna
.resize(256, 256) // resize
.quality(60) // set JPEG quality
.greyscale() // set greyscale
.write("lena-small-bw.jpg"); // save
});
// Using promises:
Jimp.read("lenna.png")
.then((lenna) => {
return lenna
.resize(256, 256) // resize
.quality(60) // set JPEG quality
.greyscale() // set greyscale
.write("lena-small-bw.jpg"); // save
})
.catch((err) => {
console.error(err);
});
// Basic Usage
Jimp.read("./path/to/image.jpg")
.then((image) => {
// Do stuff with the image.
})
.catch((err) => {
// Handle an exception.
});
Jimp.read("http://www.example.com/path/to/lenna.jpg")
.then((image) => {
// Do stuff with the image.
})
.catch((err) => {
// Handle an exception.
});
Jimp.read(jimpInstance)
.then((image) => {
// Do stuff with the image.
})
.catch((err) => {
// Handle an exception.
});
Jimp.read(buffer)
.then((image) => {
// Do stuff with the image.
})
.catch((err) => {
// Handle an exception.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment