diff --git a/README.md b/README.md index 3059d5b..7294d7e 100755 --- a/README.md +++ b/README.md @@ -1,33 +1,30 @@ -## ascii ![npm](https://badge.fury.io/js/ascii.png) +## Ascii ![npm](https://badge.fury.io/js/ascii.png) convert jpg/png/gif to ascii arts based on node-canvas ![screenshot](http://ww3.sinaimg.cn/mw1024/61ff0de3gw1e903qhbyt8j20kl0f5768.jpg) ### Installation -```` +```bash $ npm install ascii -```` +``` ### Example -````javascript +```javascript var Ascii = require('ascii'); var pic = new Ascii('demo.png'); // output in terminal (terminal mode) pic.convert(function(err, result) { - console.log(result); + console.log(result); }); // output as html strings with css style (html mode) pic.convert('html', function(err, html){ - console.log(html); - // then save it - require('fs').writeFileSync('demo.html',html); + console.log(html); + // then save it + require('fs').writeFileSync('demo.html',html); }); -```` - -### API -check this file: `index.js` +``` ### Contributing - Fork this repo diff --git a/ascii.js b/ascii.js deleted file mode 100755 index 5667977..0000000 --- a/ascii.js +++ /dev/null @@ -1,63 +0,0 @@ -// 感谢这篇文章的启发 [基于canvas将图片转化成字符画](http://www.cssha.com/img2txt-canvas) - -var Ascii = { - style: "", - // 按照不同的终端输出 - types: { - cli: { - br: '\n', - blank: ' ' - }, - html: { - br: '
', - blank: ' ' - } - }, - // 根据灰度生成相应字符 - toText: function(type, g) { - var self = this; - if (g <= 30) { - return '#'; - } else if (g > 30 && g <= 60) { - return '&'; - } else if (g > 60 && g <= 120) { - return '$'; - } else if (g > 120 && g <= 150) { - return '*'; - } else if (g > 150 && g <= 180) { - return 'o'; - } else if (g > 180 && g <= 210) { - return '!'; - } else if (g > 210 && g <= 240) { - return ';'; - } else { - return self.types[type].blank; - } - }, - // 根据rgb值计算灰度 - getGray: function(r, g, b) { - return 0.299 * r + 0.578 * g + 0.114 * b; - }, - // 初始化 - init: function(type, ctx, pic) { - var self = this, - data = ctx.getImageData(0, 0, pic.width, pic.height), - text = ''; - for (h = 0; h < data.height; h += 12) { - var p = ''; - for (w = 0; w < data.width; w += 6) { - var index = (w + data.width * h) * 4; - var r = data.data[index + 0]; - var g = data.data[index + 1]; - var b = data.data[index + 2]; - var gray = self.getGray(r, g, b); - p += self.toText(type, gray); - } - p += self.types[type].br; - text += p; - } - return (type === 'html') ? self.style + "
" + text + '
' : text; - } -} - -module.exports = Ascii; \ No newline at end of file diff --git a/demo/demo.jpg b/example/demo.jpg similarity index 100% rename from demo/demo.jpg rename to example/demo.jpg diff --git a/demo/demo2.jpg b/example/demo2.jpg similarity index 100% rename from demo/demo2.jpg rename to example/demo2.jpg diff --git a/demo/demo.html b/example/index.html similarity index 100% rename from demo/demo.html rename to example/index.html diff --git a/demo/index.js b/example/index.js similarity index 100% rename from demo/index.js rename to example/index.js diff --git a/index.js b/index.js index 9f9c332..315bfb2 100755 --- a/index.js +++ b/index.js @@ -6,48 +6,41 @@ // // @author: [turing](http://guoyu.me); // @desc: convert pictures to ascii arts based on node-canvas - -var fs = require('fs'), - Canvas = require('canvas'), - ascii = require('./ascii'); -var Ascii = function(src, params) { - this.src = src; +var fs = require('fs'); +var Canvas = require('canvas'); +var ascii = require('./lib/ascii'); + +module.exports = Ascii; + +function Ascii(src, params) { + this.src = src; } Ascii.prototype.load = function(callback) { - if (this.src) { - fs.readFile(this.src, callback); - } else { - callback(new Error('src picture required.')) - } -} + if (this.src) + return fs.readFile(this.src, callback); + return callback(new Error('src picture required.')) +}; Ascii.prototype.convert = function(type, callback) { - var t = (type && typeof(type) === 'string') ? type : 'cli', - cb = (typeof(type) === 'function' && !callback) ? type : callback; - this.load(function(err, img){ - if (!err) { - var pic = new Canvas.Image; - pic.src = img; - var cv = new Canvas(pic.width, pic.height), - ctx = cv.getContext('2d'); - ctx.drawImage(pic, 0, 0, pic.width, pic.height); - cb(null, ascii.init(t, ctx, pic)); - } else { - cb(err); - } - }); -} - -Ascii.fromBuffer = function (buffer) { - var instance = new Ascii(); - - instance.load = function (callback) { - callback(null, buffer); - }; - - return instance; -} + var t = (type && typeof(type) === 'string') ? type : 'cli'; + var cb = (typeof(type) === 'function' && !callback) ? type : callback; + this.load(function(err, img) { + if (err) return cb(err); + var pic = new Canvas.Image; + pic.src = img; + var cv = new Canvas(pic.width, pic.height); + var ctx = cv.getContext('2d'); + ctx.drawImage(pic, 0, 0, pic.width, pic.height); + cb(null, ascii.init(t, ctx, pic)); + }); +}; -module.exports = Ascii; +Ascii.fromBuffer = function(buffer) { + var instance = new Ascii(); + instance.load = function(callback) { + callback(null, buffer); + }; + return instance; +}; diff --git a/lib/ascii.js b/lib/ascii.js new file mode 100755 index 0000000..736d039 --- /dev/null +++ b/lib/ascii.js @@ -0,0 +1,63 @@ +// 感谢这篇文章的启发 [基于canvas将图片转化成字符画](http://www.cssha.com/img2txt-canvas) + +var Ascii = { + style: "", + // 按照不同的终端输出 + types: { + cli: { + br: '\n', + blank: ' ' + }, + html: { + br: '
', + blank: ' ' + } + }, + // 根据灰度生成相应字符 + toText: function(type, g) { + var self = this; + if (g <= 30) { + return '#'; + } else if (g > 30 && g <= 60) { + return '&'; + } else if (g > 60 && g <= 120) { + return '$'; + } else if (g > 120 && g <= 150) { + return '*'; + } else if (g > 150 && g <= 180) { + return 'o'; + } else if (g > 180 && g <= 210) { + return '!'; + } else if (g > 210 && g <= 240) { + return ';'; + } else { + return self.types[type].blank; + } + }, + // 根据rgb值计算灰度 + getGray: function(r, g, b) { + return 0.299 * r + 0.578 * g + 0.114 * b; + }, + // 初始化 + init: function(type, ctx, pic) { + var self = this, + data = ctx.getImageData(0, 0, pic.width, pic.height), + text = ''; + for (h = 0; h < data.height; h += 12) { + var p = ''; + for (w = 0; w < data.width; w += 6) { + var index = (w + data.width * h) * 4; + var r = data.data[index + 0]; + var g = data.data[index + 1]; + var b = data.data[index + 2]; + var gray = self.getGray(r, g, b); + p += self.toText(type, gray); + } + p += self.types[type].br; + text += p; + } + return (type === 'html') ? self.style + "
" + text + '
' : text; + } +} + +module.exports = Ascii; diff --git a/package.json b/package.json index 7355d9e..8c20a59 100755 --- a/package.json +++ b/package.json @@ -1,11 +1,8 @@ { "name": "ascii", - "version": "0.0.1", + "version": "0.1.0", "description": "convert pictures to ascii arts based on node-canvas", "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, "repository": { "type": "git", "url": "git://github.com/turingou/ascii.git" @@ -15,7 +12,7 @@ "ascii-arts", "canvas" ], - "author": "turing", + "author": "turing ", "license": "MIT", "bugs": { "url": "https://github.com/turingou/ascii/issues"