
- 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 (PIL) 中,使用 **ImageOps** 模組的 **expand()** 方法可以增加影像的尺寸,在影像周圍新增邊框或填充。這對於新增邊框、建立特定尺寸的畫布或調整影像大小以適應特定縱橫比等多種用途都很有幫助。
expand() 方法
**expand()** 方法可用於為影像新增邊框並調整其尺寸,同時保持縱橫比。我們可以自定義大小和邊框顏色以滿足我們的特定需求。
以下是 expand() 方法的基本語法:
PIL.Image.expand(size, fill=None)
其中:
**size** - 指定擴充套件影像的新尺寸(即寬度和高度)的元組。
**fill (可選)** - 用於填充邊框區域的可選顏色值。它應指定為顏色元組 (R, G, B) 或表示顏色的整數值。
以下是本章所有示例中使用的輸入影像。

示例
在這個示例中,我們使用 **expand()** 方法建立帶有邊框的擴充套件影像。
from PIL import Image, ImageOps #Open an image image = Image.open("Images/hand writing.jpg") #Define the new dimensions for the expanded image new_width = image.width + 40 #Add 40 pixels to the width new_height = image.height + 40 #Add 40 pixels to the height #Expand the image with a white border expanded_image = ImageOps.expand(image, border=20, fill="red") #Save or display the expanded image expanded_image.save("output Image/expanded_output.jpg") open_expand = Image.open("output Image/expanded_output.jpg") open_expand.show()
輸出

示例
這是另一個示例,我們使用 **Image** 模組的 **expand()** 方法使用藍色擴充套件影像邊框。
from PIL import Image, ImageOps #Open an image image = Image.open("Images/hand writing.jpg") #Define the new dimensions for the expanded image new_width = image.width + 40 #Add 40 pixels to the width new_height = image.height + 40 #Add 40 pixels to the height #Expand the image with a white border expanded_image = ImageOps.expand(image, border=100, fill="blue") #Save or display the expanded image expanded_image.save("output Image/expanded_output.jpg") open_expand = Image.open("output Image/expanded_output.jpg") open_expand.show()
輸出

廣告