- PySimpleGUI Tutorial
- PySimpleGUI - Home
- PySimpleGUI - Introduction
- PySimpleGUI - Environment Setup
- PySimpleGUI - Hello World
- PySimpleGUI - Popup Windows
- PySimpleGUI - Window Class
- PySimpleGUI - Element Class
- PySimpleGUI - Events
- PySimpleGUI - Menubar
- PySimpleGUI - Matplotlib Integration
- PySimpleGUI - Working with PIL
- PySimpleGUI - Debugger
- PySimpleGUI - Settings
- PySimpleGUI Useful Resources
- PySimpleGUI - Quick Guide
- PySimpleGUI - Useful Resources
- PySimpleGUI - Discussion
PySimpleGUI - 使用 PIL
Python Imaging Library 是一个免费的、跨平台的、开源的 Python 编程语言库,具有打开、操作和保存多种不同图像文件格式的功能。
要安装它,请使用 PIP 命令,如下所示 -
pip3 install pillow
在下面的示例中,我们使用 PIL 函数获取 PNG 图像的字节值,并将其显示在 PySimpleGUI 窗口上的 Image 元素中。
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()
它将产生以下输出窗口 -
