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()

它將生成以下 輸出視窗 -

Working with PIL
廣告