Pysimplegui 简明教程
PySimpleGUI - Working with PIL
Python Imaging Library 是 Python 编程语言的一个免费、跨平台开源库,它具有打开、处理和保存许多不同图像文件格式的功能。
Python Imaging Library is a free, cross-platform and open-source library for the Python programming language that has the functionality for opening, manipulating, and saving many different image file formats.
要安装此应用,使用 PIP 命令,如下所示 −
To install it, use the PIP command as follows −
pip3 install pillow
在以下示例中,我们使用 PIL 函数获取 PNG 图像的字节值,然后在 PySimpleGUI 窗口上的图像元素中显示相同的字节值。
In the following example, we obtain the byte value of the PNG image with PIL function and display the same in Image element on a PySimpleGUI window.
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()
它将生成以下 output 窗口 −
It will produce the following output window −