Python|使用枕头裁剪图像
在本文中,我们将学习使用枕头库裁剪图像。裁剪图像意味着选择图像内部的矩形区域并删除矩形之外的所有内容。为了裁剪图像,我们对图像对象使用crop()
方法。
Syntax : IMG.crop(box_tuple)
Parameters :
Image_path- Location of the image
IMG- Image to crop
box_tuple- [left, up, right, bottom] of the image to crop
Returns : An Image object which represents the cropped image.
示例 1:
# import Image module
from PIL import Image
# open the image
Image1 = Image.open('D:/cat.jpg')
# crop the image
croppedIm = Image1.crop((130, 120, 200, 200))
# show the image
croppedIm.show()
输入图像:
输出 :
示例 2:
# import Image module
from PIL import Image
# open the image
Image1 = Image.open('D:/cat.jpg')
# crop the image
croppedIm = Image1.crop((130, 50, 250, 150))
# show the image
croppedIm.show()
输入图像:
输出 :