libvips CLI 输出到 Windows stdout 的支持格式

Supported formats for libvips CLI output to Windows stdout

我使用 libvipsHEIC 图像转换为更易于处理的格式,并将结果通过管道传输到另一个进程而不写入磁盘。我可以使用 PNG 作为中间格式来做到这一点:

vips copy input.heic .png

然而,我链中的下一个进程只接受 BMP 图像或原始 RGB 数据。如果我在上面的命令中将 .png 替换为 .bmp 我会收到此错误:

input.heic: bad seek to 1811903
VipsForeignSave: ".bmp" is not a known target format

许多其他格式都会发生这种情况,包括原生 .vips。如果我写入磁盘而不是 stdout.

,则转换适用于所有格式

这将有助于转换为 BMP 或带有 RGB 信息的整数列表。

您可以通过 vips -l 查看支持的格式集。对于 8.10,它是:

$ vips -l | grep _target
          VipsForeignSaveCsvTarget (csvsave_target), save image to csv (.csv), priority=0, mono
          VipsForeignSaveMatrixTarget (matrixsave_target), save image to matrix (.mat), priority=0, mono
          VipsForeignSavePpmTarget (ppmsave_target), save to ppm (.ppm, .pgm, .pbm, .pfm), priority=0, rgb
          VipsForeignSaveRadTarget (radsave_target), save image to Radiance target (.hdr), priority=0, rgb
          VipsForeignSavePngTarget (pngsave_target), save image to target as PNG (.png), priority=0, rgba
          VipsForeignSaveJpegTarget (jpegsave_target), save image to jpeg target (.jpg, .jpeg, .jpe), priority=0, rgb-cmyk
          VipsForeignSaveWebpTarget (webpsave_target), save image to webp target (.webp), priority=0, rgba-only
          VipsForeignSaveHeifTarget (heifsave_target), save image in HEIF format (.heic, .heif, .avif), priority=0, rgba-only

.v.raw 可能会在 8.11 中添加。 .bmp 由 imagemagick 而不是 libvips 编写,可能无法实现。

另一种选择是使用类似 Python 界面的东西,pyvips,而不是 CLI。例如:

import os
import pyvips

image = pyvips.Image.black(10, 10)
memory = image.write_to_memory()
os.write(1, memory)

将以二进制模式将原始字节(在本例中为 100 个零)写入标准输出。

要改用 BMP,您可以这样写:

memory = image.magicksave_buffer(format="BMP")

不确定您是在寻找 work-around,还是希望 John 为 libvips 提供软件更新,或者究竟是什么。

无论如何,我只是想说,如果您想要 work-around 将 vips 输出转换为 BMP,您可以使用 ppmtobmp,它是 NetPBM 套件的一部分。

因此,文件将是:

vips copy image.heic .ppm | ppmtobmp - > result.bmp

并作为流过滤器,无需进入磁盘:

vips copy image.jpg .ppm | ppmtobmp | NextProcess

请注意,ppm 格式实际上是 RGB,开头有 3-4 行 ASCII header,其中包含尺寸 - 试试看。所以,如果你能在 Windows 中找到删除 3-4 行 ASCII 的方法,你就可以获得 RGB。或者,如果您的图像在 3 bytes/pixel 处为 640x480 像素,也许您可​​以在 Windows 上找到一种方法来获取文件的最后 (640x480x3) 个字节,或者流式传输并丢弃 PPM header方式。

关键字:HEIC、vips、NetPBM、BMP