retrography retrography - 15 days ago 5
Python Question

Shrinking image by pixel-binning using VIPS

I need to shrink some images by factor of 2 or 3 using VIPS. This has to be achieved through simple pixel binning (pixel averaging), with no additional smoothing or interpolation.

VIPS offers several functions at different levels of abstraction for resizing images (resize, shrink, reduce, affine, etc.). It also offers different interpolation methods with these functions.

I wonder what combination of functions and methods leads to simple pixel binning in VIPS. A rudimentary example would be very helpful.

Answer

You need shrink. With integer arguments, it does simple block averaging of pixels.

im = Vips.Image.new_from_file("somefile.jpg")
im = im.shrink(2, 2)
im.write_to_file("someotherfile.tif")

The pixel format stays the same, so for a uchar image, you'll get uchar averages, rounded to the nearest value. Cast to float, shrink, and round yourself if you want something different.

I'll add a note to the introduction to that section of the manual explaining what shrink and reduce do.

Comments