How to use jimp

Comprehensive jimp code examples:

How to use jimp.quality:

1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
.then(img => {
    console.log(` getting ${fileName} map image...`);
    return img.quality(70) // set JPEG quality
        .write(outputPath + ".jpg"); // save
}).then(async () => {
    console.log(" Jimp.quality(70).write()");

    await sleep(100);
    // del old local resource if exist
    if (fs.existsSync(outputPath)) { fs.unlinkSync(outputPath); }

How to use jimp.distance:

1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
case '!imgdiff':{
  if (msg.attachments.length===2&&msg.attachments[0].content_type.startsWith('image/')&&msg.attachments[1].content_type.startsWith('image/')){
    var attachmentsUrls = msg.attachments.map((u)=>{return u.proxy_url})
    jimp.read(attachmentsUrls[0], (err,img1)=>{
      jimp.read(attachmentsUrls[1], (err,img2)=>{
        var distance = jimp.distance(img1,img2)
        var diff = jimp.diff(img1,img2)
        var newMsg = 'Image 1 hash: `'+img1.hash()+'`\nImage 2 hash: `'+img2.hash()+'`\nHash Distance: `'+distance+'`\nImage Difference: `'+diff.percent*100+' %`'
        diff.image.getBuffer(jimp.MIME_PNG, (err,buffer)=>{
          if(err){debugLog(err)}

How to use jimp.default:

421
422
423
424
425
426
427
428
429
430
let usr = await prisma_default.user.findFirst({ where: { id: query.id } });
if (import_lodash5.default.isNull(usr))
  return { status: 404 };
let imgname = `${(0, import_uuid.v1)()}${import_path.default.extname(body.icon.filename)}`;
let imgpath = import_path.default.resolve(API_UPLOAD_DIR, "profile-images", imgname);
import_jimp.default.read(await body.icon.toBuffer()).then((img) => {
  img.scaleToFit(128, 128);
  img.writeAsync(imgpath);
});
prisma_default.events.create({ data: {

How to use jimp.intToRGBA:

300
301
302
303
304
305
306
307
308
  console.log("Processed " + (((i + 1) / this.width) * 100) + "%");
  for (let j = 0; j < this.height; j++) {
    let color = this.image.getPixelColor(i, j);
    var importance = this.importances.importances.find(el => el.x === i && el.y === j);
    importance = (importance) ? importance.importance : 255;
    color = Jimp.intToRGBA(color);
    this.pixels.push({ coords: [i, j], absCoords: [parseInt(this.x) + parseInt(i), parseInt(this.y) + parseInt(j)], color: color, converted: this.colors.convertColor(color), importance: importance });
  }
}

How to use jimp.diff:

1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
if (msg.attachments.length===2&&msg.attachments[0].content_type.startsWith('image/')&&msg.attachments[1].content_type.startsWith('image/')){
  var attachmentsUrls = msg.attachments.map((u)=>{return u.proxy_url})
  jimp.read(attachmentsUrls[0], (err,img1)=>{
    jimp.read(attachmentsUrls[1], (err,img2)=>{
      var distance = jimp.distance(img1,img2)
      var diff = jimp.diff(img1,img2)
      var newMsg = 'Image 1 hash: `'+img1.hash()+'`\nImage 2 hash: `'+img2.hash()+'`\nHash Distance: `'+distance+'`\nImage Difference: `'+diff.percent*100+' %`'
      diff.image.getBuffer(jimp.MIME_PNG, (err,buffer)=>{
        if(err){debugLog(err)}
        try{bot.createMessage(msg.channel.id, newMsg, {file: buffer, name: 'imgdiff.png'})}catch(err){debugLog(err)}

How to use jimp.rgbaToInt:

107
108
109
110
111
112
113
114
115
116
}

getImportantByHeatmap() {
  if (!this.colored.length) { return []; }
  this.colored.forEach((pixel, i) => {
    let importance = 255 + (pixel.color / Jimp.rgbaToInt(this.color.r, this.color.g, this.color.b, 255) * 20);
    this.importances.push({ x: pixel.x, y: pixel.y, importance: importance, heatmapImp: importance });
  });
  this.importances.sort(this.importanceSorter);
  console.log(this.importances[0]);

How to use jimp.loadFont:

1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
var newMsg='<@'+msg.author.id+'> used `!text`'
if (!creditsDisabled){
  chargeCredits(msg.author.id,0.05)
  newMsg+=', it cost :coin:`0.05`/`'+creditsRemaining(msg.author.id)+'`'
}
jimp.loadFont(jimp.FONT_SANS_32_BLACK).then(font => {
  //var newTxtWidth=jimp.measureText(jimp.FONT_SANS_32_BLACK, newTxt)
  //var newTxtHeight=jimp.measureTextHeight(jimp.FONT_SANS_32_BLACK, newTxt, 100)
  var txtObj={text:newTxt,alignmentX:jimp.HORIZONTAL_ALIGN_CENTER,alignmentY:jimp.VERTICAL_ALIGN_TOP,background:0xffffff}
  switch(c){

How to use jimp.read:

1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
case '!crop':{
  if (msg.attachments.length>0&&msg.attachments[0].content_type.startsWith('image/')){
    var attachmentsUrls = msg.attachments.map((u)=>{return u.proxy_url})
    attachmentsUrls.forEach((url)=>{
      log('cropping '+url)
      jimp.read(url,(err,img)=>{
        if(err){log('Error during cropping'.bgRed);log(err)}
        img.autocrop()
        var newMsg='<@'+msg.author.id+'> used `!crop`'
        if(!creditsDisabled){