What is the usage of images in Python?

In Python, handling images typically requires the use of third-party libraries, with Pillow being the most popular image processing library. Pillow library offers numerous functions for opening, manipulating, and saving images.

Here are some basic usage examples of the Pillow library:

  1. Install the Pillow library.
pip install Pillow
  1. Open and display the image.
from PIL import Image

# 打开图像文件
image = Image.open("example.jpg")

# 显示图像
image.show()
  1. Resize the image:
# 调整图像大小
new_size = (200, 200)
resized_image = image.resize(new_size)

# 保存调整后的图像
resized_image.save("resized_image.jpg")
  1. Convert image format.
# 转换图像格式
image.save("converted_image.png")
  1. Apply a filter effect:
from PIL import ImageFilter

# 添加模糊滤镜
blurred_image = image.filter(ImageFilter.BLUR)

# 保存处理后的图像
blurred_image.save("blurred_image.jpg")

These are simply some basic usage examples of the Pillow library, you can utilize more functionalities to process and manipulate images based on your specific needs.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds