
- 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 Imaging Library,現在稱為 Pillow)中使用顏色建立影像,涉及建立填充特定顏色的新影像。在 Pillow 中使用顏色建立影像,需要生成特定大小的影像並用所需顏色填充它。此過程允許我們生成純色影像,這對於建立背景、佔位符或簡單圖形等各種用途非常有用。
在 Pillow 中,我們有名為 `new()` 的方法,用於使用顏色建立影像。在開始時,我們已經看到了 `Image` 模組中可用的 `new()` 方法的語法和引數。
使用定義的顏色建立影像需要遵循幾個步驟。讓我們逐一檢視它們。
匯入必要的模組
要使用 Pillow,我們需要匯入所需的模組,通常是 `Image` 和 `ImageDraw`。`Image` 模組提供用於建立和操作影像的函式,而 `ImageDraw` 用於在影像上繪製形狀和文字。
定義影像大小和顏色
確定我們要建立的影像的尺寸(即寬度和高度),並指定要使用的顏色。顏色可以透過多種方式定義,例如 RGB 元組或顏色名稱。
使用指定的大小和顏色建立一個新影像
使用 `Image.new()` 方法建立一個新影像。我們可以指定影像模式,可以是“RGB”、“RGBA”、“L”(灰度)以及其他模式,具體取決於我們的需求。我們還可以為影像提供大小和顏色。
可選:在影像上繪製
如果我們想向影像新增形狀、文字或其他元素,則可以使用 `ImageDraw` 模組。這允許我們使用各種方法(如 `draw.text()`、`draw.rectangle()` 等)在影像上繪製。
儲存或顯示影像
我們可以使用 `save()` 方法將建立的影像儲存到特定格式(例如 PNG、JPEG)的檔案中。或者,我們可以使用 `show()` 方法顯示影像,該方法會在預設影像檢視器中開啟影像。
示例
在這個示例中,我們使用 `Image` 模組的 `new()` 方法建立一個純紅色影像。
from PIL import Image, ImageDraw #Define image size (width and height) width, height = 400, 300 #Define the color in RGB format (e.g., red) color = (255, 0, 0) #Red #Create a new image with the specified size and color image = Image.new("RGB", (width, height), color) #Save the image to a file image.save("output Image/colored_image.png") #Show the image (opens the default image viewer) image.show()
輸出

示例
在這個示例中,我們使用了可選功能,即使用 `ImageDraw` 模組的 `Draw()` 方法新增文字。
from PIL import Image, ImageDraw #Define image size (width and height) width, height = 400, 300 #Define the color in RGB format (e.g., red) color = (255, 0, 0) #Red #Create a new image with the specified size and color image = Image.new("RGB", (width, height), color) #Optional: If you want to draw on the image, use ImageDraw draw = ImageDraw.Draw(image) draw.text((10, 10), "Hello, Welcome to Tutorialspoint", fill=(255, 255, 255)) #Draw white text at position (10, 10) #Save the image to a file image.save("output Image/colored_image.png") #Show the image (opens the default image viewer) image.show()
輸出
