Warm tip: This article is reproduced from stackoverflow.com, please click
image-processing python deepzoom vips

libvips Nearest Neighbor / Bicubic Deep Zoom Pyramid Creation

发布于 2020-03-30 21:12:25

I'm in the process of moving some of my code off of openzoom.py and onto Libvips but am not sure how dictate the interpolation method, which is important. I at the very least need to be able to use bicubic/bilinear in one case and nearest neighbors in the other case.

My old code is as follows:

creator = deepzoom.ImageCreator(tile_size=128, tile_overlap=2, tile_format="png",
                                        image_quality=0.8, resize_filter="nearest")
creator.create(sourceFile, destFile)

Currently, using pyvips I have the following

image = pyvips.Image.new_from_file(sourceFile)
image.dzsave(destFile, tile_size=128, overlap=2,
                     suffix='.png[Q=80]')

Any help would be greatly appreciated :)

Questioner
Simon Warchol
Viewed
68
jcupitt 2020-01-31 16:50

By default, dzsave will average each 2x2 block of pixels, which is equivalent to bilinear.

Sometimes, for example with images where pixel values represent labels rather than intensity, you need a non-interpolatory downsize. For these cases, you can use the region_shrink parameter to select median or mode, which will both preserve label values.

I would use:

image = pyvips.Image.new_from_file(sourceFile, access='sequential')
image.dzsave(destFile,
             overlap=1,
             tile_size=126,
             region_shrink='mode',
             suffix='.png')

Don't forget to set the access hint. It'll give you a huge improvement in speed and memory behaviour for large images that don't support random access.

The PNG Q number sets quantization quality when outputting palettized images. Perhaps you mean compression? libvips defaults to 6, the PNG standard.

Are you sure you want overlap=2? The deepzoom standard is overlap 1. Overlap 1 means there is one pixel extra around the edge of every tile, so tiles in the centre of the image will share two pixels on every edge with their neighbours. Setting overlap=2 means you'll have four pixel overlaps, confusingly.

Likewise, tile_size=128 means most of your tiles will be 132x132 pixels. It doesn't matter for PNG, but JPG works best with multiples of 8 on an axis. I would set tile_size to (128 - 2 * overlap), as deepzoom does by default.

git master libvips adds max, min and nearest (always pick the top-left pixel) as well. A branch has lanczos3, but it was never merged for various reasons.