Quick Start: Installation and Usage Guide of Pillow Library
Abstract: Pillow is a powerful image processing library in Python and has been widely used in the field of image processing. This article will show you how to install the Pillow library and how to use it to perform common image processing operations.
1. Install the Pillow library
The installation of the Pillow library is very simple. You can complete the installation through the following steps:
Run the following command to install the Pillow library:
pip install pillow
If you have not installed pip yet, please install pip first.
2. Use the Pillow library
The Pillow library provides a series of functions and classes for image processing operations. The following are examples of some common operations:
Open image file
from PIL import Image # 打开图像文件 img = Image.open('example.jpg')
Resize image
# 调整图像大小为指定尺寸 resized_img = img.resize((800, 600)) resized_img.save('resized.jpg')
Rotate image
# 旋转图像 rotated_img = img.rotate(90) rotated_img.save('rotated.jpg')
Image scaling
# 等比缩放图像 scaled_img = img.thumbnail((400, 400)) scaled_img.save('scaled.jpg')
Crop image
# 裁剪图像 cropped_img = img.crop((100, 100, 300, 300)) cropped_img.save('cropped.jpg')
Add watermark
# 添加水印 from PIL import ImageDraw, ImageFont # 加载字体 font = ImageFont.truetype('Arial.ttf', 36) draw = ImageDraw.Draw(img) draw.text((10, 10), 'Watermark', font=font) img.save('watermarked.jpg')
Convert image Format
# 转换图像格式 img_png = img.convert('RGBA') # 转换为 PNG 格式 img_png.save('converted.png')
The above examples are only the use of some functions in the Pillow library. For more functions, please refer to the official Pillow documentation.
Conclusion:
This article introduces how to install the Pillow library and how to use the Pillow library for common image processing operations. Pillow provides a rich set of features to meet a variety of image processing needs. I hope this article can help you quickly get started using the Pillow library and achieve better results in the field of image processing.
The above is the detailed content of Pillow Library: Easy Installation and Usage Guide. For more information, please follow other related articles on the PHP Chinese website!