Search code examples
node.jsimagevips

Appending images with node sharp (libvips)


I want to use VIPS to append a directory of many smaller images in to one massive image. The node module "sharp" uses libvips. Is there any way to use sharp to append 2 images together? VIPS has a "LRJOIN" function but I don't see a sharp implementation of it.

I really just to know the fastest possible way to have VIPS append a directory of images to one big TIFF. The image is too big to use ImageMagick etc. because of memory issues.

Edit:

I used ruby-vips to join the images and call the VIPS command line tool to generate the DZI.

#!/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")

I found the code on a ruby-sharp github issue and modified it a bit. The results (just the joining part) for 550 4096x256 images:

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

Solution

  • If Ruby or Python are OK, you could try them. For example:

    #!/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])
    

    Joining 100 2000x2500 rgb tif images needs 1gb of memory and 30s or so on this desktop:

    $ 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
    

    Most of the time is obviously spent in disc IO.