📜  Python PIL | Image.crop() 方法

📅  最后修改于: 2022-05-13 01:54:32.734000             🧑  作者: Mango

Python PIL | Image.crop() 方法

PIL 是Python Imaging Library,它为Python解释器提供了图像编辑功能。 PIL.Image.crop() 方法用于裁剪任何图像的矩形部分。

代码#1:

Python3
# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"C:\Users\Admin\Pictures\geeks.png")
 
# Size of the image in pixels (size of original image)
# (This is not mandatory)
width, height = im.size
 
# Setting the points for cropped image
left = 5
top = height / 4
right = 164
bottom = 3 * height / 4
 
# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))
 
# Shows the image in image viewer
im1.show()


Python3
# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"C:\Users\Admin\Pictures\network.png")
 
# Setting the points for cropped image
left = 155
top = 65
right = 360
bottom = 270
 
# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))
 
# Shows the image in image viewer
im1.show()


原始图像

输出:


代码#2:

Python3

# Importing Image class from PIL module
from PIL import Image
 
# Opens a image in RGB mode
im = Image.open(r"C:\Users\Admin\Pictures\network.png")
 
# Setting the points for cropped image
left = 155
top = 65
right = 360
bottom = 270
 
# Cropped image of above dimension
# (It will not change original image)
im1 = im.crop((left, top, right, bottom))
 
# Shows the image in image viewer
im1.show()

原图:

输出: