前幾天介紹了一個簡單的截圖模塊——iamges,雖然簡單,但是功能還是有很多局限的地方。
jimp的優勢:1.簡單,2.支持回調方式和ES6(promise)語法也可以鏈式調用 3. 豐富的api 4.無需安裝任何程序(gm和canvas都需要)
jimp的回調用法如下:
1 var Jimp = require("jimp"); 2 3 Jimp.read("lenna.png", function (err, lenna) { 4 if (err) throw err; 5 lenna.resize(256, 256) 6 .quality(60) 7 .greyscale() 8 .write("lena-small-bw.jpg"); 9 });
promise用法:
1 var Jimp = require("jimp"); 2 3 Jimp.read("lenna.png").then(function (lenna) { 4 lenna.resize(256, 256) 5 .quality(60) 6 .greyscale() 7 .write("lena-small-bw.jpg"); 8 }).catch(function (err) { 9 console.error(err); 10 });
因為代碼是運行在node平台,個人強烈建議使用promise的寫法。
更多的用法可以參考github:https://github.com/oliver-moran/jimp,我把一些api復制下來

/* Resize */ image.contain( w, h[, mode] ); // scale the image to the given width and height, some parts of the image may be letter boxed image.cover( w, h[, mode] ); // scale the image to the given width and height, some parts of the image may be clipped image.resize( w, h[, mode] ); // resize the image. Jimp.AUTO can be passed as one of the values. image.scale( f[, mode] ); // scale the image by the factor f image.scaleToFit( w, h[, mode] ); // scale the image to the largest size that fits inside the given width and height // An optional resize mode can be passed with all resize methods. /* Crop */ image.autocrop(); // automatically crop same-color borders from image (if any) image.crop( x, y, w, h ); // crop to the given region /* Composing */ image.blit( src, x, y[, srcx, srcy, srcw, srch] ); // blit the image with another Jimp image at x, y, optionally cropped. image.composite( src, x, y ); // composites another Jimp image over this iamge at x, y image.mask( src, x, y ); // masks the image with another Jimp image at x, y using average pixel value image.rotate) and when writing formats that don't support alpha channels /* Flip and rotate */ image.flip( horz, vert ); // flip the image horizontally or vertically image.mirror( horz, vert ); // an alias for flip image.rotate( deg[, mode] ); // rotate the image clockwise by a number of degrees. Optionally, a resize mode can be passed. If `false` is passed as the second parameter, the image width and height will not be resized. // JPEG images with EXIF orientation data will be automatically re-orientated as appropriate. /* Colour */ image.brightness( val ); // adjust the brighness by a value -1 to +1 image.contrast( val ); // adjust the contrast by a value -1 to +1 image.dither565(); // ordered dithering of the image and reduce color space to 16-bits (RGB565) image.greyscale(); // remove colour from the image image.invert(); // invert the image colours image.normalize(); // normalize the channels in an image /* Alpha channel */ image.fade( f ); // an alternative to opacity, fades the image by a factor 0 - 1. 0 will haven no effect. 1 will turn the image image.opacity( f ); // multiply the alpha channel by each pixel by the factor f, 0 - 1 image.opaque(); // set the alpha channel on every pixel to fully opaque image.background( hex ); // set the default new pixel colour (e.g. 0xFFFFFFFF or 0x00000000) for by some operations (e.g. image.contain and /* Blurs */ image.gaussian( r ); // Gaussian blur the image by r pixels (VERY slow) image.blur( r ); // fast blur the image by r pixels /* Effects */ image.posterize( n ); // apply a posterization effect with n level image.sepia(); // apply a sepia wash to the image
利用這些api可以幫助我們完成大部分功能。下面還有一些特殊的東西。
1. 在圖片上書寫文字
1 Jimp.loadFont( path ).then(function (font) { 2 image.print(font, x, y, str); 3 });
2. 讀取圖片字節流
1 image.getBuffer( buffer , cb )
3. 圖片上某一點像素的操作
image.getPixelColor(x, y) //獲取圖片上某一點的像素值 image.setPixelColor(hex, x, y); //設置圖片上的某一點像素
4. 工具函數
Jimp.rgbaToInt(r, g, b, a); //將十進制轉化為十六進制 Jimp.intToRGBA(hex); //將十六進制轉化為十進制
5. 判斷圖片是否為同一張圖
Jimp使用的了pHash算法對圖片進行了計算,我們可以利用這個特點進行圖片的比較,避免服務器保存了相同的圖片
海明威碼判別
Jimp.distance(image1, image2); //返回值為相似程度,0表示兩張圖完全一樣
像素比較法
var diff = Jimp.diff(image1, image2, threshold); diff.image; //一張用來展示兩張圖片不一樣的地方的圖片 diff.percent; //像素不相同的比例
其中參數中threshold的取值為0-1,代表了比較的嚴格程度,0表示最嚴格,實際中可以結合使用兩種方式來判斷。