
- 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 - ImageOps.Colorize() 函式
PIL.ImageOps.colorize() 函式用於將灰度影像著色。它計算一個顏色楔形,將源影像中的所有黑色畫素對映到第一種顏色,將所有白色畫素對映到第二種顏色。如果指定了中間色 (mid),則使用三色對映。黑白引數應作為 RGB 元組或顏色名稱提供。可以選擇透過包含 mid 引數來進行三色對映。對於每種顏色,可以使用諸如 blackpoint 的引數定義對映位置,該引數表示一個整數,指示應將相應顏色對映到的位置。必須為這些引數保持邏輯順序,確保 blackpoint 小於或等於 midpoint,而 midpoint 小於或等於 whitepoint(如果指定了 mid)。
語法
以下是該函式的語法:
PIL.ImageOps.colorize(image, black, white, mid=None, blackpoint=0, whitepoint=255, midpoint=127)
引數
以下是該函式引數的詳細資訊:
image - 要著色的灰度影像。
black - 用於黑色輸入畫素的顏色,指定為 RGB 元組或顏色名稱。
white - 用於白色輸入畫素的顏色,指定為 RGB 元組或顏色名稱。
mid - 用於中間色輸入畫素的顏色,指定為 RGB 元組或顏色名稱(三色對映可選)。
blackpoint - 整數值 [0, 255],表示黑色顏色的對映位置。
whitepoint - 整數值 [0, 255],表示白色顏色的對映位置。
midpoint - 整數值 [0, 255],表示中間色的對映位置(三色對映可選)。
返回值
該函式返回表示著色結果的影像。
示例
示例 1
這是一個使用基本引數的 ImageOps.colorize() 函式使用方法示例。
from PIL import Image, ImageOps # Open a grayscale image gray_image = Image.open("Images/flowers_1.jpg").convert("L") # Define colors for mapping red = (255, 0, 0) cyan = (0, 255, 255) # Apply colorize operation colorized_image = ImageOps.colorize(gray_image, black=red, white=cyan) # Display the original and colorized images gray_image.show() colorized_image.show()
輸出
輸入影像

輸出影像

示例 2
此示例演示了使用可選的三色對映對灰度影像進行著色。
from PIL import Image, ImageOps # Open a grayscale image gray_image = Image.open("Images/Car_2.jpg").convert("L") # Define colors for mapping black_color = 'yellow' white_color = 'red' mid_color = 'cyan' # color name (optional for three-color mapping) # Define mapping positions blackpoint = 50 whitepoint = 200 midpoint = 127 # Apply colorize operation colorized_image = ImageOps.colorize(gray_image, black_color, white_color, mid_color, blackpoint, whitepoint, midpoint) # Display the original and colorized images gray_image.show() colorized_image.show()
輸出
輸入影像

輸出影像

示例 3
此示例演示如何使用不同的顏色和對映位置應用 colorize 函式以實現獨特的著色結果。
from PIL import Image, ImageOps # Open a grayscale image gray_image = Image.open("Images/Car_2.jpg").convert("L") # Define colors for mapping (RGB tuple) warm_black = (10, 10, 10) cool_white = (200, 220, 255) neutral_gray = (128, 128, 128) # Define mapping positions blackpoint = 30 whitepoint = 200 midpoint = 120 # Apply colorize operation with custom colors and mapping positions colorized_image = ImageOps.colorize(gray_image, warm_black, cool_white, neutral_gray, blackpoint, whitepoint, midpoint) # Display the original and colorized images gray_image.show() colorized_image.show()
輸出
輸入影像

輸出影像

python_pillow_function_reference.htm
廣告