
- 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()
它将生成以下 输出窗口 -

广告