
- 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()
輸入影像

輸出反轉影像

廣告