
- 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 中,更改影像模式是指將影像從一種顏色表示轉換為另一種顏色的過程。每種模式代表影像中編碼和解釋顏色資訊的不同方式。
更改影像模式對於各種用途很有用,例如準備用於特定應用(如列印、顯示或分析)的影像。它允許我們調整影像的顏色表示,以更好地滿足我們的需求。
在 Pillow 中,Image 類提供了一個名為 convert() 的方法,該方法允許我們更改影像的模式。影像的模式決定了它可以包含的畫素值的型別和深度。
以下是 Image 類 convert() 方法的語法和引數。
original_image.convert(mode)
其中,
original_image 這是我們要更改其模式的源影像。
mode 這是一個字串,指定新影像所需的模式。
以下是常見的更改影像模式。
L - 8 位畫素表示黑白
RGB - 3x8 位畫素表示真彩色
RGBA - 4x8 位畫素表示具有透明度的真彩色
CMYK - 4x8 位畫素表示色彩分離
HSV - 色相、飽和度、明度顏色空間
1 - 1 位畫素,黑白,每個位元組儲存一個畫素
以下是本章所有示例中使用的輸入影像。

示例
在此示例中,我們透過將 mode 引數作為 L 傳遞給 convert() 方法,將影像模式更改為黑白。
from PIL import Image #Open an image original_image = Image.open("Images/rose.jpg") #Convert the image to grayscale (mode 'L') grayscale_image = original_image.convert("L") #Save the resulting image grayscale_image.save("output Image/output_grayscale.jpg") grayscale_image.show()
輸出

示例
以下是使用 convert() 方法將影像模式更改為 1 的另一個示例。
from PIL import Image #Open an image original_image = Image.open("Images/rose.jpg") #Convert the image to RGBA mode single_image = original_image.convert("1") #Save the resulting image single_image.save("output Image/output_single_image.jpg") single_image.show()
輸出

廣告