前几天介绍了一个简单的截图模块——iamges,虽然简单,但是功能还是有很多局限的地方。

jimp的优势:1.简单,2.支持回调方式和ES6(promise)语法也可以链式调用 3. 丰富的api  4.无需安装任何程序(gm和canvas都需要)

jimp的回调用法如下:

 var Jimp = require("jimp");

 Jimp.read("lenna.png", function (err, lenna) {
if (err) throw err;
lenna.resize(256, 256)
.quality(60)
.greyscale()
.write("lena-small-bw.jpg");
});

promise用法:

 var Jimp = require("jimp");

 Jimp.read("lenna.png").then(function (lenna) {
lenna.resize(256, 256)
.quality(60)
.greyscale()
.write("lena-small-bw.jpg");
}).catch(function (err) {
console.error(err);
});

因为代码是运行在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.  在图片上书写文字

 Jimp.loadFont( path ).then(function (font) {
image.print(font, x, y, str);
});

2. 读取图片字节流

 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表示最严格,实际中可以结合使用两种方式来判断。

node平台截取图片模块——jimp的更多相关文章

  1. node上截取图片工具 images(node-images)

    我们经常会遇到服务器上传的图片进行裁剪或者增加logo等等一些操作,在node平台上该如何实现呢? 看到大家都在使用"gm"这个工具,功能很强大,但是在Windows平台上简直就是 ...

  2. Java开源生鲜电商平台-财务系统模块的设计与架构(源码可下载)

    Java开源生鲜电商平台-财务系统模块的设计与架构(源码可下载) 前言:任何一个平台也好,系统也好,挣钱养活团队这个是无可厚非的,那么对于一个生鲜B2B平台盈利模式( 查看:http://www.cn ...

  3. 电子商务(电销)平台中系统设置模块(SysSetting)数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 邮件服务器 (sys_smtp_server)|-- 自动编号|-- SMTP服务器地址 (host ...

  4. 电子商务(电销)平台中商品模块(Product)数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 商品表 (product)|-- 自动编号 (product_id)|-- 商品名称 (produc ...

  5. 电子商务(电销)平台中内容模块(Content)数据库设计明细

    以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 文章表 (article)|-- 自动编号|-- 文章标题 (title)|-- 文章类别编号 (c ...

  6. 电子商务(电销)平台中商品模块(Product)数据库设计明细(转载)

    电子商务(电销)平台中商品模块(Product)数据库设计明细 以下是自己在电子商务系统设计中的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 商品表 (product)|-- 自 ...

  7. node爬虫 -- 网页图片

    相信大家都听说过爬虫,我们也听说过Python是可以很方便地爬取网络上的图片,但是奈何本人不会Python,就只有通过 Node 来实践一下了.   接下来看我如何 板砖 ! !!   

  8. Web 前端模块出现的原因,以及 Node.js 中的模块

    模块出现原因 简单概述 随着 Web 2.0 时代的到来,JavaScript 不再是以前的小脚本程序了,它在前端担任了更多的职责,也逐渐地被广泛运用在了更加复杂的应用开发的级别上. 但是 JavaS ...

  9. 利用Node.js的Net模块实现一个命令行多人聊天室

    1.net模块基本API 要使用Node.js的net模块实现一个命令行聊天室,就必须先了解NET模块的API使用.NET模块API分为两大类:Server和Socket类.工厂方法. Server类 ...

随机推荐

  1. 解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法

    方式一:Coding JavaScript <!--[if lt IE9]> <script> (function() { var e = "abbr, articl ...

  2. java读取项目中文件路径及乱码解决

    this.getClass.getResource(path).getPath(); 如果出现中文乱码,可以使用java.net.URLDecoder.decode方法进行处理 如:URLDecode ...

  3. 在Unity中使用TDD - 初探

    描述 Editor Tests Runner是Unity中用来实现TDD的,其内部实现是基于NUnit. 其他 测试脚本要放在Editor文件夹中,测试要能够在一帧的时间内完成. 使用 打开Edito ...

  4. Unity学习疑问记录之Awake和Update

    Awake() 当一个脚本实例被载入时Awake被调用. Awake用于在游戏开始之前初始化变量或游戏状态.在脚本整个生命周期内它仅被调用一次.Awake在所有对象被初始化之后调用,所以你可以安全的与 ...

  5. x86指令集同频性能提升

    x86近5000条指令,迄今为止最复杂的指令集.这里不研究CISC & RISC,也不考虑process制程变化,主要是看最近几代IA架构对于同频率下性能的提升. x86指令集nasm文档 h ...

  6. ThinkPHP 3.2.3 使用 Swift Mailer 邮件系统发送邮件

    SwiftMailer 下载地址:https://github.com/swiftmailer/swiftmailer 版本:swiftmailer-5.x 把压缩包解压到 /ThinkPHP/Lib ...

  7. 简单快速部署samba服务器

    samba是一种在linux环境运行的免费软件,可以为局域网内的不同计算机系统之间提供文件以及打印机等资源的共享服务. samba服务安装和配置: 1.安装gcc编译器以及samba服务和samba依 ...

  8. poj2488骑士马走

    #include<stdio.h> #include<stdlib.h> int data[100][100] = {0}; int Dx[8] = {-1,1,-2,2,-2 ...

  9. Maven的依赖和传递性质

    1. 引入项目所需jar包 Maven项目直白的一大特点就是一般情况下不需要去自行下载jar包以及目标jar包的依赖包并导入,只需要在去Maven的中央仓库http://mvnrepository.c ...

  10. js == 与 === 的区别,‘’与“”的区别

    js == 与 === 的区别 1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较"转化成同一类型后的值"看"值&quo ...