當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。