
- PySimpleGUI 教程
- PySimpleGUI - 主頁
- PySimpleGUI - 簡介
- PySimpleGUI - 環境設定
- PySimpleGUI - Hello World
- PySimpleGUI - 彈出視窗
- PySimpleGUI - 視窗類
- PySimpleGUI - 元素類
- PySimpleGUI - 事件
- PySimpleGUI - 選單欄
- PySimpleGUI - Matplotlib 整合
- PySimpleGUI - 使用 PIL
- PySimpleGUI - 偵錯程式
- PySimpleGUI - 設定
- PySimpleGUI 實用資源
- PySimpleGUI - 快速指南
- PySimpleGUI - 實用資源
- PySimpleGUI - 討論
PySimpleGUI - 使用 PIL
Python 影像庫是一個免費、跨平臺、開源庫,用於 Python 程式語言,它具有開啟、處理和儲存多種不同影像檔案格式的功能。
要安裝它,請按如下方式使用 PIP 命令 -
pip3 install pillow
在以下示例中,我們使用 PIL 函式獲取 PNG 影像的位元組值,並將其顯示在 PySimpleGUI 視窗的影像元素中。
import PySimpleGUI as sg import PIL.Image import io import base64 def convert_to_bytes(file_or_bytes, resize=None): img = PIL.Image.open(file_or_bytes) with io.BytesIO() as bio: img.save(bio, format="PNG") del img return bio.getvalue() imgdata = convert_to_bytes("PySimpleGUI_logo.png") layout = [[sg.Image(key='-IMAGE-', data=imgdata)]] window = sg.Window('PIL based Image Viewer', layout,resizable=True) while True: event, values = window.read() if event == sg.WIN_CLOSED: break window.close()
它將生成以下 輸出視窗 -

廣告