English 中文(简体)
PySimpleGUI - Working with PIL
  • 时间:2024-11-05

PySimpleGUI - Working with PIL


Previous Page Next Page  

Python Imaging Library is a free, cross-platform and open-source pbrary for the Python programming language that has the functionapty for opening, manipulating, and saving many different image file formats.

To install it, use the PIP command as follows −


pip3 install pillow

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

It will produce the following output window −

Working with PIL Advertisements