使用 Pillow 庫裁剪影像


在本教程中,我們將使用 Pillow 庫裁剪影像。為此,我們將使用 crop() 函式。該函式採用左、上、右、下畫素座標來裁剪影像。

原影像

演算法

Step 1: Import Image from Pillow.
Step 2: Read the image.
Step 3: Crop the image using the crop function.
Step 4: Display the output.

示例程式碼

from PIL import Image

im = Image.open('testimage.jpg')
width, height = im.size

left = 5
top = height / 2
right = 164
bottom = 3 * height / 2

im1 = im.crop((left, top, right, bottom))
im1.show()

輸出

更新時間: 2021-03-17

233 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

入門
廣告
© . All rights reserved.