- 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 - 颜色反转
Python Pillow 中的颜色反转是一种流行的照片效果,它通过将图像的颜色反转为色轮上的互补色调来转换图像。它会导致诸如黑色变为白色,白色变为黑色以及其他颜色变化。
此技术也称为图像反转或颜色取反,是图像处理中一种系统地改变图像中颜色的一种方法。在颜色反转的图像中,颜色以这样的方式转换,即明亮区域变暗,黑暗区域变亮,并且颜色在整个颜色范围内反转。
将颜色反转应用于图像
在 Python Pillow 中,颜色反转是通过反转整个图像光谱中的颜色来实现的。该库在其 ImageOps 模块 中提供了 **invert()** 函数,允许您将颜色反转应用于图像。此函数旨在反转给定图像的颜色,有效地应用颜色反转效果。该方法的语法如下:
PIL.ImageOps.invert(image)
其中,
**image** - 这是要反转的输入图像。
示例
以下示例使用 PIL.ImageOps.invert() 函数创建输入图像的反转版本。
from PIL import Image
import PIL.ImageOps
# Open an image
input_image = Image.open('Images/butterfly.jpg')
# Create an inverted version of the image
inverted_image = PIL.ImageOps.invert(input_image)
# Display the input image
input_image.show()
# Display the inverted image
inverted_image.show()
输入图像
输出反转图像
将颜色反转应用于 RGBA 图像
虽然 **ImageOps** 模块中的大多数函数都设计用于处理 L(灰度)和 RGB 图像。当您尝试将此反转函数应用于具有 RGBA 模式(包括用于透明度的 Alpha 通道)的图像时,它确实会引发 OSError,指出它不支持该图像模式。
示例
以下是一个示例,演示了如何在处理透明度时使用 RGBA 图像。
from PIL import Image
import PIL.ImageOps
# Open an image
input_image = Image.open('Images/python logo.png')
# Display the input image
input_image.show()
# Check if the image has an RGBA mode
if input_image.mode == 'RGBA':
# Split the RGBA image into its components
r, g, b, a = input_image.split()
# Create an RGB image by merging the red, green, and blue components
rgb_image = Image.merge('RGB', (r, g, b))
# Invert the RGB image
inverted_image = PIL.ImageOps.invert(rgb_image)
# Split the inverted image into its components
r2, g2, b2 = inverted_image.split()
# Merge the inverted RGB image with the original alpha channel to maintain transparency
final_transparent_image = Image.merge('RGBA', (r2, g2, b2, a))
# Show the final transparent image
final_transparent_image.show()
else:
# Invert the image for non-RGBA images
inverted_image = PIL.ImageOps.invert(input_image)
inverted_image.show()
输入图像
输出反转图像
广告