来自命令行的Imagemagick压缩导致TIFF的缓冲区错误

时间:2018-04-04 12:50:50

标签: node.js command-line compression tiff imagemagick-convert

我正在尝试使用ImageMagick命令行通过从节点生成子进程来压缩TIFF文件。某些命令不会引起任何问题,例如单色和去斑点。

      let tiff2png = spawn('C:\\Program Files\\ImageMagick-7.0.7-Q8\\convert.exe', [
    path,
    '-monochrome',
    '-normalize',
    '-despeckle',
    targetPath]);

这不会引起任何错误,但当源文件大约为2.5MB时输出17MB的TIFF文件。我试过添加

-compress BZip
在保存到targetPath之前

到我的args,并在转换后遍历targetPaths。两次尝试都会从以下事件监视器中注销缓冲区错误:

  tiff2png.stderr.on('data', (data: any) => {
    console.log('stderr: ', JSON.stringify(data));
  });

我已经将错误字符串化以扩展错误对象,希望看到一些有用的细节。完整错误只包含一种“缓冲区”和一个uselss数字数组:

stderr:  {"type":"Buffer","data":[99,111,110,118,101,114,116,46,101,120,101,58,32,117,110,114,101,99,111,103,110,105,122,101,100,32,111,112,116,105,111,110,32,96,45,99,111,109,112,114,101,115,115,32,66,90,105,112,39,32,64,32,101,114,114,111,114,47,99,111,110,118,101,114,116,46,99,47,67,111,110,118,101,114,116,73,109,97,103,101,67,111,109,109,97,110,100,47,49,50,51,57,46,13,10]}

某些其他ImageMagick命令会抛出相同的错误,例如-contrast-stretch和-level。这是项目的最后一个元素,过去两天我一直在尝试让它工作。这是在64位Win10 Ultimate机器上,因此命令中的Windows路径。

我尝试在writeFileSync中包装输出,但没有成功。我有一种感觉,这是一个文件流问题,但我无法辨别确切的性质。如果有人能帮助我找到有关错误或解决方案的详细信息,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

您可以使用以下方式列出所有压缩选项:

identify -list compress

示例输出

B44
B44A
BZip
DXT1
DXT3
DXT5
Fax
Group4
JBIG1
JBIG2
JPEG
JPEG2000
Lossless
LosslessJPEG
LZMA
LZW
None
Piz
Pxr24
RLE
Zip
RunlengthEncoded
ZipS

如果您将其输入tiffdump,您可以在TIFF标记中看到哪些结果会产生1以外的值:

for f in $(identify -list compress); do 
   echo $f
   convert -size 1024x768 xc:red +noise random -compress $f a.tif
   tiffdump a.tif | grep -i "^Compression"
done

B44
Compression (259) SHORT (3) 1<1>
B44A
Compression (259) SHORT (3) 1<1>
BZip
Compression (259) SHORT (3) 1<1>
DXT1
Compression (259) SHORT (3) 1<1>
DXT3
Compression (259) SHORT (3) 1<1>
DXT5
Compression (259) SHORT (3) 1<1>
Fax
Compression (259) SHORT (3) 1<3>
Group4
Compression (259) SHORT (3) 1<4>
JBIG1
convert: CompressionNotSupported `JBIG1' @ error/tiff.c/WriteTIFFImage/3590.
Compression (259) SHORT (3) 1<1>
JBIG2
Compression (259) SHORT (3) 1<1>
JPEG
Compression (259) SHORT (3) 1<7>
JPEG2000
Compression (259) SHORT (3) 1<1>
Lossless
Compression (259) SHORT (3) 1<1>
LosslessJPEG
Compression (259) SHORT (3) 1<1>
LZMA
convert: CompressionNotSupported `LZMA' @ error/tiff.c/WriteTIFFImage/3590.
Compression (259) SHORT (3) 1<1>
LZW
Compression (259) SHORT (3) 1<5>
None
Compression (259) SHORT (3) 1<1>
Piz
Compression (259) SHORT (3) 1<1>
Pxr24
Compression (259) SHORT (3) 1<1>
RLE
Compression (259) SHORT (3) 1<32773>
Zip
Compression (259) SHORT (3) 1<8>
RunlengthEncoded
Compression (259) SHORT (3) 1<32773>
ZipS
Compression (259) SHORT (3) 1<1>

对我来说,至少在我的环境中,对于使用ImageMagick的TIFF唯一有效的压缩类型是:

FAX, GROUP4, JPEG, RLE, LZW or ZIP

此外,-monochrome以及-normalize对我来说似乎是多余的,因为前者意味着&#34;只使用黑人和白人&#34; ,后者意味着&#34;将像素范围扩展到黑白的范围&#34;

此外,请查看Wikipedia,据我所见,BZIP也未提及。