
- 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 - 影像顏色
在 Pillow(Python 影像庫)中,影像上的顏色被定義為紅色 (R)、綠色 (G) 和藍色 (B) 分量的組合,通常稱為 **RGB 顏色模型**。此模型將顏色表示為這三種原色的混合,每個分量的範圍從 0(最小強度)到 255(最大強度)。通常還會包含第四個分量 Alpha (A) 用於表示透明度,其中 0 表示完全透明,255 表示完全不透明。
在處理影像時,準確定義顏色至關重要,因為它決定了影像的外觀和構成。Pillow 提供了多種表示和定義顏色方法,使其能夠靈活地用於各種影像處理和操作任務。
以下是這些顏色分量的詳細說明:
紅色 (R)
紅色分量表示顏色中紅色的數量。
當 R 為 0 時,表示沒有紅色,從而產生青色或綠色的色調。
當 R 為 255 時,表示紅色強度最大,產生純紅色。
中間值會產生從粉紅色到橙色的不同紅色色調。
綠色 (G)
綠色分量表示顏色中綠色的數量。
當 G 為 0 時,表示沒有綠色,從而產生品紅色或藍色的色調。
當 G 為 255 時,表示綠色強度最大,產生純綠色。
中間值會產生從青檸色到藍綠色的不同綠色色調。
藍色 (B)
藍色分量表示顏色中藍色的數量。
當 B 為 0 時,表示沒有藍色,從而產生黃色或紅色的色調。
當 B 為 255 時,表示藍色強度最大,產生純藍色。
中間值會產生從海軍藍到天藍色的不同藍色色調。
Alpha (A)
Alpha 分量是可選的,但對於控制透明度至關重要。
當 A 為 0 時,表示畫素完全透明,允許其後面的內容顯示出來。
當 A 為 255 時,表示畫素完全不透明,它完全覆蓋了下面的內容。
中間 Alpha 值會建立不同級別的透明度,允許畫素部分透明。
為了在 Pillow 中表示顏色,我們通常使用一個元組或列表,其中包含四個值,順序為 (R, G, B, A),以指定畫素的顏色。例如,**(255, 0, 0, 255)** 表示一個完全不透明的紅色畫素,而 **(0, 255, 0, 128)** 表示一個半透明的綠色畫素。
示例
讓我們看看使用 Python Pillow 和 RGBA 顏色表示建立影像的示例。
from PIL import Image # Create an RGBA image with Semi-transparent green image = Image.new('RGBA', (700, 300), (0, 255, 0, 128)) # Display the resultant Semi-transparent green image image.show()
執行上述程式後,您將獲得如下所示的 RGBA 輸出:

瞭解 RGB 顏色模型和 Alpha 分量是使用 Pillow 進行影像處理和操作的基礎,因為它允許我們以各種方式建立、修改和組合顏色和影像。
十六進位制顏色表示
十六進位制顏色程式碼在 Web 和圖形設計中被廣泛使用。在 Pillow 中,我們可以使用十六進位制字串定義顏色,該字串表示 RGB 值。格式為 **#RRGGBB**,其中 RR、GG 和 BB 分別是紅色、綠色和藍色的兩位十六進位制值。
示例
以下是如何使用 Python Pillow **十六進位制**顏色表示建立影像的示例。在此示例中,我們正在建立具有 80% 不透明度的紅色影像。
from PIL import Image # Create a red colored image with 80% opacity image = Image.new('RGBA', (700, 300), "#ff0000cc") # Display the resultant image image.show()
執行上述程式後,您將獲得以下輸出:

命名顏色
Pillow ImageColor 模組提供了一組命名顏色(常用的 HTML 顏色名稱),允許我們使用這些顏色名稱而不是數值。例如,名稱 red 表示純紅色,需要注意的是顏色名稱不區分大小寫,這意味著“red”和“Red”被視為相同。
示例
以下示例演示瞭如何訪問和列印 Pillow 的 **ImageColor** 模組中可用的命名顏色。
from PIL import ImageColor # Access all the named colors color_map = ImageColor.colormap # Count the number of named colors num_colors = len(color_map) # Print the available named colors and count print(color_map) print(f'Total number of named colors: {num_colors}')
執行上述程式後,您將獲得類似於以下的輸出:
{'aliceblue': '#f0f8ff', 'antiquewhite': '#faebd7', 'aqua': '#00ffff', 'aquamarine': '#7fffd4', 'azure': '#f0ffff', 'beige': '#f5f5dc', 'bisque': '#ffe4c4', 'black': '#000000', 'blanchedalmond': '#ffebcd', 'blue': '#0000ff', 'blueviolet': '#8a2be2', 'brown': '#a52a2a', 'burlywood': '#deb887', 'cadetblue': '#5f9ea0', 'chartreuse': '#7fff00', 'chocolate': '#d2691e', 'coral': '#ff7f50', 'cornflowerblue': '#6495ed', 'cornsilk': '#fff8dc', 'crimson': '#dc143c', 'cyan': '#00ffff', 'darkblue': '#00008b', 'darkcyan': '#008b8b', 'darkgoldenrod': '#b8860b', 'darkgray': '#a9a9a9', 'darkgrey': '#a9a9a9', 'darkgreen': '#006400', 'darkkhaki': '#bdb76b', 'darkmagenta': '#8b008b', 'darkolivegreen': '#556b2f', 'darkorange': '#ff8c00', 'darkorchid': '#9932cc', 'darkred': '#8b0000', 'darksalmon': '#e9967a', 'darkseagreen': '#8fbc8f', 'darkslateblue': '#483d8b', 'darkslategray': '#2f4f4f', 'darkslategrey': '#2f4f4f', 'darkturquoise': '#00ced1', 'darkviolet': '#9400d3', 'deeppink': '#ff1493', 'deepskyblue': '#00bfff', 'dimgray': '#696969', 'dimgrey': '#696969', 'dodgerblue': '#1e90ff', 'firebrick': '#b22222', 'floralwhite': '#fffaf0', 'forestgreen': '#228b22', 'fuchsia': '#ff00ff', 'gainsboro': '#dcdcdc', 'ghostwhite': '#f8f8ff', 'gold': '#ffd700', 'goldenrod': '#daa520', 'gray': '#808080', 'grey': '#808080', 'green': '#008000', 'greenyellow': '#adff2f', 'honeydew': '#f0fff0', 'hotpink': '#ff69b4', 'indianred': '#cd5c5c', 'indigo': '#4b0082', 'ivory': '#fffff0', 'khaki': '#f0e68c', 'lavender': '#e6e6fa', 'lavenderblush': '#fff0f5', 'lawngreen': '#7cfc00', 'lemonchiffon': '#fffacd', 'lightblue': '#add8e6', 'lightcoral': '#f08080', 'lightcyan': '#e0ffff', 'lightgoldenrodyellow': '#fafad2', 'lightgreen': '#90ee90', 'lightgray': '#d3d3d3', 'lightgrey': '#d3d3d3', 'lightpink': '#ffb6c1', 'lightsalmon': '#ffa07a', 'lightseagreen': '#20b2aa', 'lightskyblue': '#87cefa', 'lightslategray': '#778899', 'lightslategrey': '#778899', 'lightsteelblue': '#b0c4de', 'lightyellow': '#ffffe0', 'lime': '#00ff00', 'limegreen': '#32cd32', 'linen': '#faf0e6', 'magenta': '#ff00ff', 'maroon': '#800000', 'mediumaquamarine': '#66cdaa', 'mediumblue': '#0000cd', 'mediumorchid': '#ba55d3', 'mediumpurple': '#9370db', 'mediumseagreen': '#3cb371', 'mediumslateblue': '#7b68ee', 'mediumspringgreen': '#00fa9a', 'mediumturquoise': '#48d1cc', 'mediumvioletred': '#c71585', 'midnightblue': '#191970', 'mintcream': '#f5fffa', 'mistyrose': '#ffe4e1', 'moccasin': '#ffe4b5', 'navajowhite': '#ffdead', 'navy': '#000080', 'oldlace': '#fdf5e6', 'olive': '#808000', 'olivedrab': '#6b8e23', 'orange': '#ffa500', 'orangered': '#ff4500', 'orchid': '#da70d6', 'palegoldenrod': '#eee8aa', 'palegreen': '#98fb98', 'paleturquoise': '#afeeee', 'palevioletred': '#db7093', 'papayawhip': '#ffefd5', 'peachpuff': '#ffdab9', 'peru': '#cd853f', 'pink': '#ffc0cb', 'plum': '#dda0dd', 'powderblue': '#b0e0e6', 'purple': '#800080', 'rebeccapurple': '#663399', 'red': '#ff0000', 'rosybrown': '#bc8f8f', 'royalblue': '#4169e1', 'saddlebrown': '#8b4513', 'salmon': '#fa8072', 'sandybrown': '#f4a460', 'seagreen': '#2e8b57', 'seashell': '#fff5ee', 'sienna': '#a0522d', 'silver': '#c0c0c0', 'skyblue': '#87ceeb', 'slateblue': '#6a5acd', 'slategray': '#708090', 'slategrey': '#708090', 'snow': '#fffafa', 'springgreen': '#00ff7f', 'steelblue': '#4682b4', 'tan': '#d2b48c', 'teal': '#008080', 'thistle': '#d8bfd8', 'tomato': '#ff6347', 'turquoise': '#40e0d0', 'violet': '#ee82ee', 'wheat': '#f5deb3', 'white': '#ffffff', 'whitesmoke': '#f5f5f5', 'yellow': '#ffff00', 'yellowgreen': '#9acd32'} Total number of named colors: 148
灰度顏色
灰度顏色由一個單一的強度值表示,範圍從 0(黑色)到 255(白色)。我們可以使用單個整數值定義灰度顏色。
示例
以下是如何使用灰度顏色表示建立新的灰度影像的示例。
from PIL import Image, ImageDraw, ImageFont # Create a greyscale image with color intensity value image = Image.new('L', (700, 300), 100) #Create a drawing object draw = ImageDraw.Draw(image) #Define text attributes text = "Welcome to Tutorialspoint..." font = ImageFont.truetype("arial.ttf", size=35) text_position = (150, 130) # Specify the text color using the single integer value text_color = 255 #Add text to the image draw.text(text_position, text, fill=text_color, font=font) image.show()
以下是上述程式的輸出:
