I find this answer, and I want to use pyvips to resize images. In the mentioned answer and the official documentation image resized by scale. However, I want to resize the image to a specific height and width. Is there any way to achieve this with pyvips?
Asked
Active
Viewed 1,864 times
2 Answers
3
The thumbnail operation in pyvips will resize to fit an area. For example:
thumb = pyvips.Image.thumbnail("some-file.jpg", 128)
Will load some-file.jpg
and make a high-quality image that fits within 128x128 pixels.
Variations on thumbnail
can load from strings, buffers or even pipes.
The libvips docs have a chapter on vipsthumbnail explaining how to use the (many) options.

jcupitt
- 10,213
- 2
- 23
- 39
-
Thumbnail operation crop image or only resize it to the desired size? – d4riush Sep 28 '21 at 14:16
-
It resizes to fit within the box. It has a mode which will also crop if necessary, but it's not the default. Have a look at the docs. – jcupitt Sep 28 '21 at 17:12
2
You can use the more general affine
function.
An affine transformation does scaling
, rotation
and translation
all at once. You can parameterize the affine matrix to do 0 rotation and 0 translation.
You first calculate the necessary factors to achieve the target shape
img = pyvips.Image.new_from_file("...")
height_factor = target_height / img.height
width_factor = target_width / img.width```
and apply them via
img.affine((width_factor, 0, 0, height_factor))

0-_-0
- 1,313
- 15
- 15