Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rescaling an image with libvips

I have an image which is 6130x5548 pixels and I want to rescale it so that the longest side is 32768 pixels (and then do a pyramid of tiles with 7 zoom levels). I undestand vips resize is the obvious way for something like that, hence I tried the line below

vips resize image_in.tif img_rescaled.tif 5.345513866231648

The number 5.34551 is just the ratio 32768/6130, the scale factor along my x axis. If I want to specify the exact dimensions in pixels of the retured image how can I do that please?

I tried to use vips thumbnail for this purpose, I dont know if this is recommended or not but it does work.

vips thumbnail image_in.tif img_rescaled.tif 32768

Is something like that ok please?

Also the two approaches give quite different outputs in terms of MB size. While vips thumbnail produces a tif with size 2.8Gb the vips resize call returns a tif with size 1.8Gb.

Both images have (obviously) the same dimensions 32768x29657 pixels, same resolution 72dpi but different bit depth The tif from vips thumbnail has 24 bit depth whereas the one from vips resize 16 bit depth. The original image has bit depth=16.

Also, I understand that the algorithm used by vips translate plays a significant role to the resulting file size. Can I set the algorithm when I use vips thumbnail and/or the bit depth please?

like image 810
Aenaon Avatar asked May 23 '19 16:05

Aenaon


People also ask

How do you proportionally resize an image?

To maintain the object's proportions, press and hold SHIFT while you drag the sizing handle. To both maintain the object's proportions and keep its center in the same place, press and hold both CTRL and SHIFT while you drag the sizing handle.


1 Answers

resize only takes a scale factor, so you need to calculate it. You can use something like:

width=$(vipsheader -f width somefile.tif)
height=$(vipsheader -f height somefile.tif)
size=$((width > height ? width : height))
factor=$(bc <<< "scale=10; 32768 / $size")
vips resize somefile.tif huge.tif $factor

I'd go to 8-bit before upscaling, since you only need 8 bits for the display. You can use:

vips colourspace thing.tif other.tif srgb

To make an 8-bit srgb version.

bash gets so ugly when you start doing stuff like this that I'd be tempted to switch to pyvips.

import pyvips

image = pyvips.Image.new_from_file('somefile.tif', access='sequential')
image = image.colourspace('srgb')
image = image.resize(32768 / max(image.width, image.height))
image.dzsave('mypyramid')

It has the extra advantage that it won't use any temporary files. pyvips builds pipelines of image processing operations, so that program will stream pixels from your input, upsize them, and write the pyramid all at the same time, and all in parallel. It won't use much memory and it'll be quick.

like image 74
jcupitt Avatar answered Oct 06 '22 23:10

jcupitt