在macOS上執行ImageMagick
前言:
本教程講的是用homebrew安裝ImageMagick,用Nodejs呼叫ImageMagick。
1.在MAC上安裝homebrew
https://brew.sh/index_zh-cn.html
2.用homebrew安裝ImageMagick
brew install ImageMagick
安裝完成後可以在終端裡輸入convert,如果跳出來一堆說明,則安裝成功
3.使用nodejs來呼叫
這裡我演示三種呼叫的方式:
- 命令列
http://www.imagemagick.org/script/command-line-tools.php
在終端裡的寫法
此處的67×57表示寬度67畫素,高度57畫素。
還可以用%的寫法,如67%x57%。
convert -resize 67x57 input_pic_path output_pic_path
我這裡使用nodejs建立子程序來執行我們的命令
var exec = require('child_process').exec;
var cmdStr = 'convert -resize 67x57 input_pic_path output_pic_path';
exec(cmdStr, function(err,stdout,stderr){
if(err) {
console.log('error: ' stderr);
} else {
console.log('success: ' stdout);
}
});
- im模組
https://www.npmjs.com/package/imagemagick
im模組是Imagemagick官方的呼叫模組,但是已經很久沒有更新過了,而且以後也不會更新
var im = require('imagemagick');
im.convert(['input_pic_path', '-resize', '25x120', 'output_pic_path'],
function(err, stdout){
if (err) throw err;
console.log('stdout:', stdout);
});
- gm
https://www.npmjs.com/package/gm
它支援GraphicsMagick和ImageMagick,並且還在持續更新中,我建議使用這種方法,因為可以報bug。
var gm = require('gm').subClass({
imageMagick: true
});
gm('input/Desert.jpg')
.resize(240, 240)
.noProfile()
.write('output/Desert.jpg', function (err) {
if (!err) console.log('done');
});
写评论
很抱歉,必須登入網站才能發佈留言。