pyvips icon indicating copy to clipboard operation
pyvips copied to clipboard

Resize both width and height

Open AntixK opened this issue 4 years ago • 7 comments

I wish to resize an image overriding the aspect ratio. I couldn't find a way to do this.

Is there a way to directly give the required size overriding the aspect ratio?

Thanks!

AntixK avatar Oct 25 '21 08:10 AntixK

Hi @AntixK ,

If it's in a file, the easiest way is with thumbnail:

x = pyvips.Image.thumbnail("something.jpg", 200, height=300, size="both")

Will size to 200x300 pixels.

If it's an image, use resize and give both an hscale and vscale.

jcupitt avatar Oct 25 '21 09:10 jcupitt

Appreciate the quick response!

I tried the thumbnail method, but it still resizes by the aspect ratio.

image

AntixK avatar Oct 25 '21 10:10 AntixK

Argh, sorry, I should not rely on memory. Try:

$ python3
Python 3.9.7 (default, Sep 10 2021, 14:59:43) 
[GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyvips
>>> x = pyvips.Image.thumbnail("k2.jpg", 200, height=300, size="force")
>>> x
<pyvips.Image 200x300 uchar, 3 bands, srgb>

jcupitt avatar Oct 25 '21 10:10 jcupitt

Another related question -

How do I set the interpolation method for resizing the image? According to docs, the default seems to be LANCZOS3.

My goal here is to get a resized image identical to the one I have with PIL. The Lanczos kernel used there seems to be slightly different (Maybe Lanczos2?).

Thank you.

AntixK avatar Oct 27 '21 01:10 AntixK

thumbnail picks the best interpolator for you -- it's supposed to do a best practice resize and not give you any options. If you want more control, you need to drop down to a lower level and call reduce directly.

I don't think you can expect identical results. Image resize is complex, with a lot of unspecified parts, and two different implementations are unlikely to handle issues like rounding and centreing in an identical way.

jcupitt avatar Oct 27 '21 05:10 jcupitt