Чтобы изменить размер изображения с помощью библиотеки Python Pillow, вы можете использовать следующие методы:
Метод 1: использование функции resize()
from PIL import Image
# Open the image
image = Image.open("image.jpg")
# Resize the image
resized_image = image.resize((width, height))
# Save the resized image
resized_image.save("resized_image.jpg")
Метод 2: использование функции thumbnail()
from PIL import Image
# Open the image
image = Image.open("image.jpg")
# Resize the image maintaining aspect ratio
image.thumbnail((width, height))
# Save the resized image
image.save("resized_image.jpg")
Метод 3: использование функции ImageOps.fit()
from PIL import Image, ImageOps
# Open the image
image = Image.open("image.jpg")
# Resize the image to fit within the specified dimensions
resized_image = ImageOps.fit(image, (width, height), Image.ANTIALIAS)
# Save the resized image
resized_image.save("resized_image.jpg")
Метод 4. Использование функции ImageOps.scale()
from PIL import Image, ImageOps
# Open the image
image = Image.open("image.jpg")
# Resize the image by scaling
resized_image = ImageOps.scale(image, scale_factor)
# Save the resized image
resized_image.save("resized_image.jpg")