
- 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 與 Tkinter 的 BitmapImage 和 PhotoImage 物件
python pillow 庫中的 ImageTk 模組提供了從 PIL 影像建立和操作 Tkinter BitmapImage 和 PhotoImage 物件的功能。本教程討論了 ImageTk 模組中關鍵類和方法的描述。
這些類和方法提供了使用 Python 中的 PIL 處理 Tkinter 影像的便捷方式,允許將影像整合到圖形使用者介面中。
Python Pillow 中的 Tkinter BitmapImage 類
ImageTk.BitmapImage 類表示一個與 Tkinter 相容的點陣圖影像,可以在任何 Tkinter 期望影像物件的地方使用。
以下是關於該類的關鍵細節:
提供的影像必須具有“1”模式。
值為 0 的畫素被視為透明。
如果提供了其他選項,則會轉發到 Tkinter。
一個常用選項是 foreground,允許指定非透明部分的顏色。
以下是 ImageTk.BitmapImage() 類的語法:
class PIL.ImageTk.BitmapImage(image=None, **kw)
其中,
image - 一個模式為“1”的 PIL 影像。值為 0 的畫素被視為透明。
**kw - 傳遞給 Tkinter 的其他選項。最常用的選項是 foreground,它指定非透明部分的顏色。
以下是 BitmapImage 類提供的方法列表:
height() - 返回影像的高度(以畫素為單位)。
width() - 返回影像的寬度(以畫素為單位)。
示例
這是一個示例,演示瞭如何建立 Tkinter 視窗,使用 PIL 載入影像,然後使用 ImageTk.BitmapImage 建立與 Tkinter 相容的影像物件,以便在 Tkinter Label 中顯示。
from tkinter import * from PIL import ImageTk, Image # Create a Tkinter window root = Tk() # Create a sample image 1-bit mode image (black and white) pil_image = Image.new("1", (700, 300), color=1) # Create a BitmapImage from the PIL image bitmap_image = ImageTk.BitmapImage(pil_image, background='', foreground='gray') # Display the BitmapImage in a Tkinter Label label = Label(root, image=bitmap_image) label.pack() # Run the Tkinter event loop root.mainloop()
輸出

Python Pillow 中的 PhotoImage 類
ImageTk.PhotoImage() 類表示 Pillow 中與 Tkinter 相容的照片影像,它適用於任何 Tkinter 期望影像物件的地方。
以下是關於該類的關鍵細節:
如果影像為 RGBA 格式,則 Alpha 值為 0 的畫素將被視為透明。
建構函式可以使用 PIL 影像或模式和大小進行初始化。或者,您可以使用 file 或 data 引數來初始化照片影像物件。
以下是 ImageTk.PhotoImage() 類的語法:
class PIL.ImageTk.PhotoImage(image=None, size=None, **kw)
其中,
image - 表示 PIL 影像或模式字串。如果使用模式字串,則還必須給出大小。
size - 如果第一個引數是模式字串,則定義影像的大小。
file - 使用 Image.open(file) 載入影像的檔名。
data - 一個 8 位字串,包含從影像檔案載入的影像資料。
以下是 PhotoImage() 類提供的方法列表:
height() - 獲取影像的高度(以畫素為單位)。
width() - 獲取影像的寬度(以畫素為單位)。
paste(im) - 將 PIL 影像貼上到照片影像中。請注意,如果顯示照片影像,這可能會很慢。
引數 - im - 一個 PIL 影像。大小必須與目標區域匹配。如果模式不匹配,則影像將轉換為點陣圖影像的模式。
示例
這是一個示例,演示瞭如何建立 Tkinter 視窗,使用 PIL 載入影像,然後使用 ImageTk.PhotoImage 建立與 Tkinter 相容的影像物件,以便在 Tkinter Label 中顯示。
from tkinter import * from PIL import ImageTk, Image # Create a Tkinter window root = Tk() # Create a canvas widget canvas = Canvas(root, width=700, height=400, bg='white') canvas.grid(row=2, column=3) # Load an image using PIL and create a PhotoImage pil_image = Image.open("Images/pillow-logo-w.jpg") tk_image = ImageTk.PhotoImage(pil_image) # Draw the image on the canvas canvas.create_image(20, 20, anchor=NW, image=tk_image) # Start the Tkinter event loop mainloop()
輸出
