6

I have a large Tiff image that I want to chop into 512x512 tiles and write to disk.

In the past I've used ImageMagick like so:

convert -crop 512x512 +repage image_in.tif image_out_%d.tif

But recently this hasn't been working, processes running out of memory, etc.

Is there a similar command in VIPS? I know there's a CLI but I can't find an example or useful explanation in the documentation, and I'm still trying to figure out the nip2 GUI thing. Any help appreciated. :)

Nicholas McCarthy
  • 346
  • 1
  • 6
  • 15

2 Answers2

12

libvips has a operator which can do this for you very quickly. Try:

$ vips dzsave wtc.tif outdir --depth one --tile-size 512 --overlap 0 --suffix .tif

That's the DeepZoom writer making a depth 1 pyramid of tif tiles. Look in outdir_files/0 for the output tiles. There's a chapter in the docs talking about how to use dzsave.

It's a lot quicker than IM for me:

$ time convert -crop 512x512 +repage huge.tif x/image_out_%d.tif
real    0m5.623s
user    0m2.060s
sys     0m2.148s
$ time vips dzsave huge.tif x --depth one --tile-size 512 --overlap 0 --suffix .tif
real    0m1.643s
user    0m1.668s
sys     0m1.000s

Where huge.tif is a 10,000 by 10,000 pixel uncompressed RGB image. Plus it'll process any size image in only a small amount of memory.

jcupitt
  • 10,213
  • 2
  • 23
  • 39
  • Is it possible to give the tiles custom names, for example including row & column or x/y coordinates? I don't see such an option in the documentation but libvips is new to me. – John May 09 '20 at 06:14
  • The tiles are named as `column_row.tif` in this example, is that what you mean? – jcupitt May 09 '20 at 10:16
  • Yes thanks, that will work well for me. My question was whether tile names can be customized in vips like you can in ImageMagick. Could one include X/Y coordinates instead of row/col, for example? I'm also wondering what happens in the overlap zone, where some programs sum or "feather" pixels. I'm hoping that vips does neither--i.e., that it just repeats the original values. – John May 10 '20 at 15:15
  • dzsave outputs deepzoom / google maps / zoomify / iiif pyramids, so you get a choice from those naming conventions. Overlaps are just pixels that are shared between tiles, so eg. with overlap 1, the rightmost two columns in tile 0_0 are identical to the leftmost two column in tile 1_0. Set overlap to 0 if you want non-overlapping tiles. – jcupitt May 10 '20 at 19:19
  • Thanks for the clarification. It seems like a very useful library. – John May 11 '20 at 01:08
1

I am running into the same issue. It seems that VIPS does not have a built-in command like the one from imagemagick above, but you can do this with some scripting (Python-code snippet):

for x in xrange(0, tiles_per_row):
    xoffset = x * tile_size
    for y in xrange(0, tiles_per_row):
        yoffset = y * tile_size
        filename = "%d_%d_%d.png" % (zoom, x, y)
        command = "vips im_extract_area %s %s %d %d %d %d" % (base_image_name, filename,  xoffset, yoffset, tile_size, tile_size)
        os.system(command)

However you won't get the same speed as with imagemagick cropping...

j0nes
  • 8,041
  • 3
  • 37
  • 40
  • As an aside, I found that base graphicsmagick worked a lot better than imagemagick in this case, more so if you fiddle with the memlimit and file writing options (sorry, I can't remember which I eventually used now). – Nicholas McCarthy Jul 25 '12 at 11:26