How to save images in a folder using Python?

To save image files, you can utilize Python’s PIL library (Python Imaging Library).

Firstly, you need to install the PIL library. You can install the PIL library in the terminal using the following command:

pip install pillow

Next, you can use the following code to save the image file:

from PIL import Image

# 打开图片文件
image = Image.open('example.jpg')

# 保存图片文件
image.save('output.jpg')

This example assumes there is an image file named example.jpg, then opens the file using the Image.open() method. After that, it saves the image as a new file named output.jpg using the save() method.

Please note that the path to save image files can include folder directories. For example, you can use image.save(‘path/to/output.jpg’) to specify the folder path for saving the file. If the folder path does not exist, Python will automatically create it.

If you want to resize or apply other image processing operations to an image before saving it, you can use various image processing methods provided by the PIL library.

bannerAds