python-pillow / Pillow

Python Imaging Library (Fork)
https://python-pillow.org
Other
12.33k stars 2.24k forks source link

Scale factor for resizing #8312

Closed enomis-dev closed 3 months ago

enomis-dev commented 3 months ago

Hi! Often working with images it is common to specify the scale factor to downsample/upsample the images preserving the aspect ratio. I was wondering if it could be possible to include a scale factor as input param of the resize method.

Today I achieve this by doing the following:

from PIL import Image

image = Image.open('example.jpg')
scale_factor = 0.5  # For example, reduce the size by half
# Calculate the new size
new_size = (
    int(image.width * scale_factor),
    int(image.height * scale_factor)
)
resampled_image = image.resize(new_size)

Perhaps, it could be interesting to have this directly inside the resize method. I leave a simple PR in case you like the addition. Wdyt?

Yay295 commented 3 months ago

Does Image.reduce work?

radarhere commented 3 months ago

So the Image.reduce code to scale an image down by half would be

from PIL import Image
with Image.open("/Tests/images/hopper.png") as im:
    reduced_image = im.reduce(2)
enomis-dev commented 3 months ago

Nice, thank you for answer, reduce does exactly that! I'll close the isssue.