Python Pillow 简明教程

Python Pillow - Color Inversion

Python Pillow 中的色彩反转是一种流行的照片效果,它通过在色轮上反转颜色使其变成互补色调来转换图像。它会导致一些变化,例如黑色变为白色,白色变为黑色,以及其他颜色偏移。

Color inversion in Python Pillow is a popular photo effect, that transforms an image by reversing the colors to their complementary hues on the color wheel. It results in changes such as black becoming white, white becoming black, and other color shifts.

此技术(也称为图像反转或颜色取反)是一种图像处理方法,它系统地改变图像中的颜色。在颜色反转的图像中,颜色的转换方式是浅色区域变深,深色区域变浅,颜色在色谱中反转。

This technique, also referred to as image inversion or color negation, is a method in image processing that systematically alters the colors within an image. In a color-inverted image, the colors are transformed in such a way that light areas become dark, dark areas become light, and colors are inverted across the color spectrum.

Applying the Color Inversion to an Image

在 Python Pillow 中,色彩反转是通过在图像光谱中反转颜色来实现的。该库在其 ImageOps module 中提供 invert() 函数,让你可以对图像应用色彩反转。此函数旨在否定给定图像的颜色,有效地应用色彩反转效果。该方法的语法如下 −

In Python Pillow, color inversion is achieved through the inversion of colors across the image spectrum. The library offers the invert() function within its ImageOps module, allowing you to apply color inversion to the images. This function is designed to negate the colors of a given image, effectively applying the color inversion effect. The syntax of the method is as follows −

PIL.ImageOps.invert(image)

其中,

Where,

  1. image − This is the input image to invert.

Example

以下示例使用 PIL.ImageOps.invert() 函数创建输入图像的反转版本。

The following example creates an inverted version of an input image using the PIL.ImageOps.invert() function.

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()
butterfly and flowers
butterfly inverted image

Applying the Color Inversion to RGBA Images

虽然 ImageOps 模块中的大多数函数都设计用于处理 L(灰度)和 RGB 图像。当你尝试将此反转函数应用于具有 RGBA 模式(包括针对透明度的 alpha 通道)的图像时,它实际上会引发 OSError,指出不支持该图像模式。

While most functions in the ImageOps module are designed to work with L (grayscale) and RGB images. When you attempt to apply this invert function to an image with RGBA mode (which includes an alpha channel for transparency), it will indeed raise an OSError stating that it is not supported for that image mode.

Example

以下示例演示如何在处理透明度的情况下使用 RGBA 图像。

Here is an example that demonstrates how to work with RGBA images while handling transparency.

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()
python logo input
inverted python logo