Skip to content

Resize both width and height #278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
AntixK opened this issue Oct 25, 2021 · 7 comments
Open

Resize both width and height #278

AntixK opened this issue Oct 25, 2021 · 7 comments

Comments

@AntixK
Copy link

AntixK commented Oct 25, 2021

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!

@jcupitt
Copy link
Member

jcupitt commented Oct 25, 2021

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.

@AntixK
Copy link
Author

AntixK commented Oct 25, 2021

Appreciate the quick response!

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

image

@jcupitt
Copy link
Member

jcupitt commented Oct 25, 2021

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>

@AntixK AntixK closed this as completed Oct 27, 2021
@AntixK AntixK reopened this Oct 27, 2021
@AntixK
Copy link
Author

AntixK commented Oct 27, 2021

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.

@jcupitt
Copy link
Member

jcupitt commented Oct 27, 2021

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 centring in an identical way.

@ogencoglu
Copy link

How do we use reduce?

Something like this (which is in line with the format in docs) gives error:
pyvips.Image.new_from_array(image).reduce(hshrink=2016/9806, vshrink=1504/6754, kernel="linear", gap=2)
where image is a numpy array.
Error:
Error: reduce needs 3 arguments, but 1 given

Reduce docs: https://libvips.github.io/pyvips/vimage.html#pyvips.Image.reduce

@jcupitt
Copy link
Member

jcupitt commented May 1, 2025

hshrink and vshrink are required arguments, not keyword arguments. Try:

pyvips.Image.new_from_array(image).reduce(2016/9806, 1504/6754, kernel="linear", gap=2)

If your image has an alpha you'll probably want to premultiply. You might need to consider gamma as well.

The main docs are here:

https://www.libvips.org/API/current/libvips-resample.html#vips-reduce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants