node平臺擷取圖片模組——jimp

向藍天招手發表於2016-06-06

前幾天介紹了一個簡單的截圖模組——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
View Code

利用這些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表示最嚴格,實際中可以結合使用兩種方式來判斷。

 

相關文章