当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python PIL Image.crop()用法及代码示例


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

用法: PIL.Image.crop(box = None)

参数:
box –定义左,上,右和下像素坐标的4元组。


返回类型:图像(将矩形区域返回为(左,上,右,下)元组)。

返回:Image对象。

代码1:

# Improting 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 orginal 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 orginal image) 
im1 = im.crop((left, top, right, bottom)) 
  
# Shows the image in image viewer 
im1.show()

原始图片

输出:


代码2:

# Improting 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 orginal image) 
im1 = im.crop((left, top, right, bottom)) 
  
# Shows the image in image viewer 
im1.show()

原始图片:

输出:



相关用法


注:本文由纯净天空筛选整理自sanjeev2552大神的英文原创作品 Python PIL | Image.crop() method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。