
- Python Pillow 教程
- Python Pillow - 首頁
- Python Pillow - 概述
- Python Pillow - 環境設定
- 基本影像操作
- Python Pillow - 影像處理
- Python Pillow - 調整影像大小
- Python Pillow - 翻轉和旋轉影像
- Python Pillow - 裁剪影像
- Python Pillow - 為影像新增邊框
- Python Pillow - 識別影像檔案
- Python Pillow - 合併影像
- Python Pillow - 影像剪裁和貼上
- Python Pillow - 滾動影像
- Python Pillow - 在影像上寫入文字
- Python Pillow - ImageDraw 模組
- Python Pillow - 連線兩張影像
- Python Pillow - 建立縮圖
- Python Pillow - 建立水印
- Python Pillow - 影像序列
- Python Pillow 顏色轉換
- Python Pillow - 影像上的顏色
- Python Pillow - 使用顏色建立影像
- Python Pillow - 將顏色字串轉換為 RGB 顏色值
- Python Pillow - 將顏色字串轉換為灰度值
- Python Pillow - 透過更改畫素值來更改顏色
- 影像處理
- Python Pillow - 降噪
- Python Pillow - 更改影像模式
- Python Pillow - 影像合成
- Python Pillow - 使用 Alpha 通道
- Python Pillow - 應用透視變換
- 影像濾鏡
- Python Pillow - 為影像新增濾鏡
- Python Pillow - 卷積濾鏡
- Python Pillow - 模糊影像
- Python Pillow - 邊緣檢測
- Python Pillow - 浮雕影像
- Python Pillow - 增強邊緣
- Python Pillow - 逆向銳化蒙版濾鏡
- 影像增強和校正
- Python Pillow - 增強對比度
- Python Pillow - 增強銳度
- Python Pillow - 增強顏色
- Python Pillow - 校正色彩平衡
- Python Pillow - 去噪
- 影像分析
- Python Pillow - 提取影像元資料
- Python Pillow - 識別顏色
- 高階主題
- Python Pillow - 建立動畫 GIF
- Python Pillow - 批次處理影像
- Python Pillow - 轉換影像檔案格式
- Python Pillow - 為影像新增填充
- Python Pillow - 顏色反轉
- Python Pillow - 使用 Numpy 進行機器學習
- Python Pillow 與 Tkinter BitmapImage 和 PhotoImage 物件
- Image 模組
- Python Pillow - 影像混合
- Python Pillow 有用資源
- Python Pillow - 快速指南
- Python Pillow - 函式參考
- Python Pillow - 有用資源
- Python Pillow - 討論
Python Pillow - 影像剪裁和貼上
剪裁影像
Pillow(Python 影像庫)允許我們從影像中提取矩形區域。提取的影像區域也稱為影像的邊界框。在crop()方法中,Image模組建立一個新的影像,表示原始影像的指定區域。此方法允許我們指定要裁剪區域的邊界框的座標。
以下是 Pillow 中'crop()'方法的語法和用法:
Image.crop(box)
其中,
box - 這是一個元組,指定我們要提取的矩形區域。box 引數應為四個值的元組:(左,上,右,下)。
left 是邊界框左邊緣的 x 座標。
upper 是邊界框上邊緣的 y 座標。
right 是邊界框右邊緣的 x 座標。
lower 是邊界框下邊緣的 y 座標。
示例
在這個示例中,我們根據需要使用Image模組的crop()方法裁剪矩形部分。
from PIL import Image #Open the image image = Image.open("Images/book.jpg") #Define the bounding box for cropping box = (100, 100, 200, 200) #(left, upper, right, lower) #Crop the image using the defined bounding box cropped_image = image.crop(box) #Save or display the cropped image cropped_image.save("output Image/cropped_image.jpg") cropped_image.show()
要裁剪的影像

輸出

示例
這是另一個使用crop()方法裁剪影像矩形部分的示例。
from PIL import Image #Open the image image = Image.open("Images/rose.jpg") #Define the bounding box for cropping box = (10, 10, 200, 200) #(left, upper, right, lower) #Crop the image using the defined bounding box cropped_image = image.crop(box) #Save or display the cropped image cropped_image.save("output Image/cropped_image.jpg") cropped_image.show()
輸入影像

輸出

貼上影像
使用 Python Pillow 貼上影像允許我們從一個影像中提取感興趣的區域並將其貼上到另一個影像上。此過程可用於影像裁剪、物件提取和合成等任務。
Pillow(Python 影像庫)中的paste()方法允許我們將一個影像貼上到另一個影像的指定位置。這是一種常用的影像合成方法,用於新增水印或將一個影像疊加到另一個影像上。
以下是paste()函式的語法和引數:
PIL.Image.paste(im, box, mask=None)
im - 這是源影像,即我們要貼上到當前影像上的影像。
box - 此引數指定我們要貼上源影像的位置。它可以是座標元組'(左,上,右,下)'。源影像將貼上到這些座標定義的邊界框內。
mask(可選) - 如果提供此引數,則它可以是定義透明蒙版的影像。貼上的影像將根據蒙版影像中的透明度值進行蒙版。
示例
以下是如何使用paste()方法將一個影像貼上到另一個影像上的示例。
from PIL import Image #Open the background image background = Image.open("Images/bw.png") #Open the image to be pasted image_to_paste = Image.open("Images/tutorialspoint.png") #Define the position where the image should be pasted position = (100, 100) #Paste the image onto the background background.paste(image_to_paste, position) #Save the modified image background.save("OutputImages/paste1.jpg") background.show()
要使用的影像


輸出

廣告