
- 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 - ImageChops.composite() 函式
ImageChops.composite() 函式用於透過使用透明蒙版混合兩個影像來建立合成影像。此函式是主 Image 模組中 composite 函式的別名。
語法
以下是該方法的語法:
PIL.ImageChops.composite(image1, image2, mask)
或者
PIL.Image.composite(image1, image2, mask)
引數
以下是其引數的詳細資訊:
image1 - 要合成的第一個輸入影像。
image2 - 要合成的第二個輸入影像。它必須與第一個影像具有相同的模式和大小。
mask - 蒙版影像。此影像可以具有“1”、“L”或“RGBA”模式,並且必須與其他兩個影像具有相同的大小。
返回值
此函式的返回型別為 Image。
示例
示例 1
在此示例中,image1、image2 和 mask 是輸入影像,composite() 函式用於根據 mask 中的透明度資訊將它們混合在一起。
from PIL import Image, ImageChops # Open the first image image1 = Image.open('Images/Light.jpg') # Open the second image with the same mode and size as the first image image2 = Image.open('Images/TP-W.jpg') # Open the mask image with mode "L" mask = Image.open("Images/mask.jpg").convert("L") # Use the composite function from ImageChops to blend the images using the mask composite_image = ImageChops.composite(image1, image2, mask) # Display or save the resulting composite image image1.show() image2.show() mask.show() composite_image.show()
輸出
輸入影像 1

輸入影像 2

輸入蒙版

輸出合成影像

示例 2
提供的示例建立一個畫素值為 128 的純灰色影像,並將其用作蒙版以將兩個影像混合在一起。
from PIL import Image, ImageChops # Open the first image image1 = Image.open('Images/TP-W.jpg') # Open the second image with the same mode and size as the first image image2 = Image.open('Images/rose.jpg').resize(image1.size) # Create a solid gray mask image with a pixel value of 128 mask = Image.new("L", image1.size, 128) # Use the composite function to blend the images using the mask composite_image = Image.composite(image1, image2, mask) # Display the input and resulting images image1.show() image2.show() mask.show() composite_image.show()
輸出
輸入影像 1

輸入影像 2

輸入蒙版

輸出合成影像

示例 3
此示例建立一個在黑色背景上帶有白色矩形的蒙版,並使用它將兩個影像混合在一起。
from PIL import Image, ImageDraw # Open the first image image1 = Image.open('Images/TP-W.jpg') # Open the second image with the same mode and size as the first image image2 = Image.open('Images/rose.jpg').resize(image1.size) # Create a mask image with a white rectangle on a black background mask = Image.new("L", image1.size, 0) draw = ImageDraw.Draw(mask) draw.rectangle((245, 45, 445, 345), fill=255) # Use the composite function to blend the images using the mask composite_image = Image.composite(image1, image2, mask) # Display the input and resulting images image1.show() image2.show() mask.show() composite_image.show()
輸出
輸入影像 1

輸入影像 2

輸入蒙版

輸出合成影像

python_pillow_function_reference.htm
廣告