正如 Fred (@fmw42) 所说,您不能单独使用 ImageMagick 来做到这一点,但应该可以使用小型 C/C++、Perl、PHP 或 Python 脚本来做一些事情。
基本上,PNG 文件很好地“分块”,参见Wikipedia PNG Specification,每个块都有一个类型 - 例如IHDR(标题)、PLTE(调色板)、@987654326 @(图像数据)、IEND(文件结束标记)以及至关重要的字节长度。
因此,您可以编写一个脚本/程序,从您的管道中读取数据,获取块并将它们附加到内存中的字符串,并一直这样做,直到它到达 IEND 块。此时,它会使用 ImageMagick 或 GD 或 Pillow 将内存中的字符串转换为 BMP 并将其写入另一个管道, 或将字符串写入 ImageMagick 正在读取的管道,并让 ImageMagick 转换文件。然后,您会将累积的 (PNG) 字符串清零并开始读取下一个文件。
我不想为 15 分编写和调试所有这些,但它看起来像这样:
chrome-cast | demultiplex
demultiplex 是这样的:
while not error
clear accumulated string of PNG data
done = false
while not done
read chunk type and length of chunk
read whole chunk and append to accumulated string of PNG data
if chunk = IEND
pass accumulated PNG string to ImageMagick to make BMP
done = true
endif
end
end
请注意,您的程序不必“理解”所有块类型或其内容 - 只需识别块长度和到达时的 IEND 块。
请注意,您可能希望运行 pngcheck 或 pngsplit 以开始了解 PNG 文件是如何按块构建的 - 它们是 here。事实上,pngsplit 无论如何都可以为所欲为。
下面是pngcheck 的示例:
pngcheck -v file.png
File: file.png (462308 bytes)
chunk IHDR at offset 0x0000c, length 13
800 x 468 image, 32-bit RGB+alpha, non-interlaced
chunk gAMA at offset 0x00025, length 4: 0.45455
chunk cHRM at offset 0x00035, length 32
White x = 0.3127 y = 0.329, Red x = 0.64 y = 0.33
Green x = 0.3 y = 0.6, Blue x = 0.15 y = 0.06
chunk bKGD at offset 0x00061, length 6
red = 0x00ff, green = 0x00ff, blue = 0x00ff
chunk pHYs at offset 0x00073, length 9: 3779x3779 pixels/meter (96 dpi)
chunk tIME at offset 0x00088, length 7: 2 Oct 2017 21:34:26 UTC
chunk IDAT at offset 0x0009b, length 32768
zlib: deflated, 32K window, maximum compression
chunk IDAT at offset 0x080a7, length 32768
chunk IDAT at offset 0x100b3, length 32768
chunk IDAT at offset 0x180bf, length 32768
chunk IDAT at offset 0x200cb, length 32768
chunk IDAT at offset 0x280d7, length 32768
chunk IDAT at offset 0x300e3, length 32768
chunk IDAT at offset 0x380ef, length 32768
chunk IDAT at offset 0x400fb, length 32768
chunk IDAT at offset 0x48107, length 32768
chunk IDAT at offset 0x50113, length 32768
chunk IDAT at offset 0x5811f, length 32768
chunk IDAT at offset 0x6012b, length 32768
chunk IDAT at offset 0x68137, length 32768
chunk IDAT at offset 0x70143, length 3115
chunk tEXt at offset 0x70d7a, length 37, keyword: date:create
chunk tEXt at offset 0x70dab, length 37, keyword: date:modify
chunk IEND at offset 0x70ddc, length 0
No errors detected in file.png (24 chunks, 69.1% compression).
我在 Perl 中做了一个基本版本的 PNG 分块以获得另一个答案,所以请快速查看 here。