在Python中加载PIL(Python Imaging Library)库,您需要先安装Pillow库,因为PIL已经更名为Pillow。以下是安装和使用Pillow的步骤:
1. 安装Pillow库:
pip install pillow
2. 在Python代码中导入Pillow库并使用:
from PIL import Image
打开图像文件
im = Image.open('image.jpg')
显示图像
im.show()
保存图像
im.save('new_image.jpg')
调整图像尺寸
new_size = (width, height)
im_resized = im.resize(new_size)
裁剪图像
box = (left, upper, right, lower)
im_cropped = im.crop(box)
转换图像模式
im_gray = im.convert('L')
以上步骤展示了如何安装Pillow库并在Python代码中导入它,然后使用Pillow的功能来处理图像。