- 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
广告