我想檢查是否有損壞的JPEG圖像,到目前爲止,直接在命令行中,我可以使用identify image.jpg
,輸出:如何使用node.js和gm檢查損壞的jpg圖像?
image.jpg JPEG 1920x1200 1920x1200+0+0 8-bit sRGB 65.5KB 0.000u 0:00.009
identify: Premature end of JPEG file `image.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
identify: Corrupt JPEG data: premature end of data segment `image.jpg' @ warning/jpeg.c/JPEGWarningHandler/352.
或 gm identify image.jpg
其輸出:
image.jpg JPEG 1920x1200+0+0 DirectClass 8-bit 64.0Ki 0.000u 0:01
gm identify: Corrupt JPEG data: premature end of data segment (image.jpg).
gm identify: Corrupt JPEG data: premature end of data segment (image.jpg).
會如果我可以使用gm package也可以獲得損壞的JPEG數據,那就太好了。只需用identify()
輸出大量的數據,但沒有關於數據損壞
gm('image.jpg')
.identify('%C',function (err, data) {
if (!err) console.log(data)
else console.error(err)
});
我在自述注意到這樣一個字條:
,如果通用汽車不能與方法提供你所需要或不( ),您可以簡單地使用gm()。in()或gm().out()來設置您自己的 參數。
我已經試過這樣的事情:
gm()
.command("identify")
.in('image.jpg');
,但我沒有得到任何輸出,所以我可能做錯了。
我也試過節點CMD:
cmd.get(
'gm identify image.jpg',
function(data){
console.log('output: ',data)
}
);
但我只看到輸出的第一行。
什麼是通過gm
包正從identify
多輸出的清潔/推薦的方法是什麼?否則,閱讀identify命令的完整輸出的節點優雅解決方案是什麼?
更新 我的猜測是使用gm
因爲它是通過stderr
,不stdout
不顯示的字符串。
var spawn = require('child_process').spawn;
var bin = "identify"
var args = ['image.jpg'];
var cspr = spawn(bin, args);
cspr.stderr.on('data', function (data) {
data += '';
console.log(data.replace("\n", "\nstderr: "));
});
cspr.on('exit', function (code) {
console.log('child process exited with code ' + code);
process.exit(code);
});
什麼是通過gm
得到stderr
輸出的清潔方式:
我使用的this snippet一個扭捏版本測試?
嗨,喬治,對不起,我不說'node',但我認爲這是非常相似的 - 也許? http://stackoverflow.com/a/43222531/2836621 –
如果問題是你想使用'identify'而不是'convert',你可以使'convert'像'identify'一樣工作(從而使用後者的調用界面)通過使用'gm convert someImage.jpg info:-'如果需要,可以選擇在'-verbose'中添加。 –
@MarkSetchell非常感謝您的輸入。我已經將你的建議調整爲節點:'''gm(jpgPath) .command('convert') .write('out.png',function(err,stdout,stderr)console.log(「stderr」 ,stderr); });'''它的工作原理!如果你添加你的評論作爲答案,我會非常樂意接受:) –