1.asciify-image
介绍: asciify-image(www.npmjs.com/package/asc…) 是一个将图片转换为 ASCII 图形的没有本地依赖的 nodejs 包。 包大小:14.4 kB
安装:
npm install asciify-image
demo: 我们花几分钟时间搭建一个 nodejs + express 的项目,来体验下 asciify-image 效果。 在终端依次执行一下命令,我们快速搭建 demo:nodejs (我们假定你已经安装了 Node.js)
mkdir nodejs
cd nodejs
npm init // 一路回车即可
执行完以上命令,即可生成一个简单的 nodejs 项目,在nodejs 项目中,分别新建文件夹 public 和 index.js 文件,项目目录结构如下:
其中,index.js 是项目主文件,在文件中添加如下代码:
var express = require('express');
var app = express();
var path = require("path")
var asciify = require('asciify-image');
var options = {
fit: 'box',
color: true,
width: 40,
height: 50
}
asciify(path.resolve(__dirname,'./public/mingzhentankenan.jpeg'), options)
.then(function (asciified) {
// Print asciified image to console
console.log(asciified);
})
.catch(function (err) {
// Print error to console
console.error(err);
});
app.get('/', function (res, rep) {
rep.send('nodejs + express 快速搭建项目');
});
app.listen(3000);
以上,打开执行终端,我们就能看到 asciify-image 的执行效果:
原图如下:
原图经过 asciify-image 转换后:
2.image-to-ascii
介绍: image-to-ascii 同样是将图像转换为ASCII ART 的 Node.js 模块。 包大小:15.9 kB
安装:
# Using npm
npm install --save image-to-ascii
# Using yarn
yarn add image-to-ascii
使用:
const imageToAscii = require("image-to-ascii");
imageToAscii("https://octodex.github.com/images/privateinvestocat.jpg", {
colored: true,
size: 10,
pixels: '祝程序员们节日快乐!'
}, (err, converted) => {
console.log(err || converted);
});
两个工具相比,asciify-image 比 image-to-ascii 包体积比 image-to-ascii 小,且不需要本地依赖。功能和参数配置项都差不多。
写这篇文章祝各位程序员节日快乐!😊😊😊