Resize Images with Python: PIL/Pillow Guide

One option is: To resize an image, you can use the PIL library (Python Imaging Library).

Firstly, make sure that the PIL library has been installed. You can install it using the following command:

pip install Pillow

Next, you can use the following code to resize the image:

from PIL import Image

# 打开图片
image = Image.open("example.jpg")

# 设置新的尺寸
new_size = (800, 600)

# 修改尺寸
resized_image = image.resize(new_size)

# 保存修改后的图片
resized_image.save("example_resized.jpg")

In this example, first open the image to be modified using the Image.open() method. Then, use the resize() method to input new dimensions and return a modified image object. Finally, use the save() method to save the modified image.

The code above will resize the dimensions of the image example.jpg to 800×600 and save it as example_resized.jpg.

bannerAds