使用 node sharp (libvips) 附加图像

Appending images with node sharp (libvips)

我想使用 VIPS 将包含许多小图像的目录附加到一个大图像中。节点模块 "sharp" 使用 libvips。有什么方法可以使用 sharp 将 2 张图像附加在一起? VIPS 有一个 "LRJOIN" 功能,但我没有看到它的清晰实现。

我真的只是想知道让 VIPS 将图像目录附加到一个大 TIFF 的最快方法。由于内存问题,图像太大无法使用ImageMagick等。

编辑:

我使用了ruby-vips拼接图片调用VIPS命令行工具生成天珠

#!/usr/bin/ruby

require 'rubygems'
require 'vips'

a = VIPS::Image.new(ARGV[1])
ARGV[2..-1].each {|name| a = a.tbjoin(VIPS::Image.tiff(name, :compression => :deflated))}                              
a.write("output.tiff", :compression => :deflated)

system("vips dzsave output.tiff '#{ARGV[0]}'/output_dz.zip --overlap=0 --suffix=.jpg")

我在 ruby-sharp github 问题上找到了代码并对其进行了一些修改。 550 张 4096x256 图像的结果(仅连接部分):

real    0m17.283s
user    0m47.045s
sys     0m2.139s

如果Ruby或Python都可以,你可以试试。例如:

#!/usr/bin/python

import sys
from gi.repository import Vips

if len(sys.argv) < 3:
    print("usage: join outfile in1 in2 in3 ...")
    sys.exit(1)

def imopen(filename):
    return Vips.Image.new_from_file(filename,
                                    access = Vips.Access.SEQUENTIAL_UNBUFFERED)

acc = imopen(sys.argv[2])
for infile in sys.argv[3:]:
    acc = acc.join(imopen(infile), Vips.Direction.HORIZONTAL,
                   align = "centre",
                   expand = True,
                   shim = 50,
                   background = 255)

acc.write_to_file(sys.argv[1])

在此桌面上加入 100 张 2000x2500 rgb tif 图片需要 1GB 内存和 30s 左右:

$ time ../join.py x.tif *.tif
real    0m36.255s
user    0m8.344s
sys 0m3.396s
$ vipsheader x.tif 
x.tif: 204950x2500 uchar, 3 bands, srgb, tiffload

大部分时间显然都花在了磁盘IO上。