'graph' 的图像显示不工作 | PySimpleGUI - sg.Graph
Image display with 'graph' not working | PySimpleGUI - sg.Graph
在我的申请中,有两个windows。第一个 'master' window1 是图像选择器,第二个 'slave' window2 是图像控制器。在选择器 window 中,用户浏览并选择包含图像的文件夹,然后选择一张图像显示在图像控制器 window 中。为了显示所选图像,我使用了 PySimpleGUI 的 sg.Graph() 功能。
但是,当我 运行 这段代码时,所选图像没有显示。白色 canvas 出现在控制器 window 中,但里面没有图像。我做错了什么?
代码:
import PySimpleGUI as sg
from PIL import Image, ImageTk
import os.path
import io
# PIL supported image types
img_types = (".png", ".jpg", "jpeg", ".tiff", ".bmp")
# ------------------------------------------------------------------------------
# use PIL to read data of one image
# ------------------------------------------------------------------------------
def get_img_data(f, maxsize=(1200, 850), first=False):
"""Generate image data using PIL
"""
img = Image.open(f)
img.thumbnail(maxsize)
if first: # tkinter is inactive the first time
bio = io.BytesIO()
img.save(bio, format="PNG")
del img
return bio.getvalue()
return ImageTk.PhotoImage(img)
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Make Window 1
# ------------------------------------------------------------------------------
def make_win1():
layout = [
[
sg.Text("Image Folder"),
sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
sg.FolderBrowse(),
],
[
sg.Listbox(values=[], enable_events=True, size=(40, 20), key="-FILE LIST-")
],
]
return sg.Window('Image Selector Window', layout, location=(800,600), finalize=True)
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Make Window 2
# ------------------------------------------------------------------------------
def make_win2():
layout = [
[
sg.Graph(
canvas_size=(1800, 900),
graph_bottom_left=(0, 0),
graph_top_right=(1800, 900),
key="-GRAPH-",
change_submits=True, # mouse click events
background_color='white',
drag_submits=True
)
]
]
return sg.Window('Image Controller Window', layout, size=(1920,1080), finalize=True)
# ------------------------------------------------------------------------------
window1, window2 = make_win1(), None # start off with 1 window open
while True: # Event Loop
window, event, values = sg.read_all_windows()
if event == sg.WIN_CLOSED or event == 'Exit':
window.close()
if window == window2: # if closing win 2, mark as closed
window2 = None
elif window == window1: # if closing win 1, exit program
break
elif event == "-FOLDER-":
folder = values["-FOLDER-"]
try:
# Get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
fnames = [f for f in file_list if os.path.isfile(os.path.join(folder, f)) and f.lower().endswith(img_types)]
window1["-FILE LIST-"].update(fnames)
elif event == "-FILE LIST-": # A file was chosen from the listbox
if not window2:
window2 = make_win2()
graph = window2.Element("-GRAPH-")
try:
filename = os.path.join(values["-FOLDER-"], values["-FILE LIST-"][0])
graph.DrawImage(data=filename, location=(0,540), size = (400,400))
except:
pass
window1.close()```
说法错误,
graph.draw_image(data=filename, location=(0,540), size = (400,400))
方法定义为
def draw_image(self, filename=None, data=None, location=(None, None)):
"""
:param filename: if image is in a file, path and filename for the image. (GIF and PNG only!)
"""
所以应该是
graph.draw_image(filename=filename, location=(0,540))
它只适用于 PNG 和 GIF 文件,您可以对不同类型的图像使用以下代码。
from io import BytesIO
from PIL import Image
im = Image.open(filanme)
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
graph.draw_image(data=data, location=(0,540))
在我的申请中,有两个windows。第一个 'master' window1 是图像选择器,第二个 'slave' window2 是图像控制器。在选择器 window 中,用户浏览并选择包含图像的文件夹,然后选择一张图像显示在图像控制器 window 中。为了显示所选图像,我使用了 PySimpleGUI 的 sg.Graph() 功能。
但是,当我 运行 这段代码时,所选图像没有显示。白色 canvas 出现在控制器 window 中,但里面没有图像。我做错了什么?
代码:
import PySimpleGUI as sg
from PIL import Image, ImageTk
import os.path
import io
# PIL supported image types
img_types = (".png", ".jpg", "jpeg", ".tiff", ".bmp")
# ------------------------------------------------------------------------------
# use PIL to read data of one image
# ------------------------------------------------------------------------------
def get_img_data(f, maxsize=(1200, 850), first=False):
"""Generate image data using PIL
"""
img = Image.open(f)
img.thumbnail(maxsize)
if first: # tkinter is inactive the first time
bio = io.BytesIO()
img.save(bio, format="PNG")
del img
return bio.getvalue()
return ImageTk.PhotoImage(img)
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Make Window 1
# ------------------------------------------------------------------------------
def make_win1():
layout = [
[
sg.Text("Image Folder"),
sg.In(size=(25, 1), enable_events=True, key="-FOLDER-"),
sg.FolderBrowse(),
],
[
sg.Listbox(values=[], enable_events=True, size=(40, 20), key="-FILE LIST-")
],
]
return sg.Window('Image Selector Window', layout, location=(800,600), finalize=True)
# ------------------------------------------------------------------------------
# ------------------------------------------------------------------------------
# Make Window 2
# ------------------------------------------------------------------------------
def make_win2():
layout = [
[
sg.Graph(
canvas_size=(1800, 900),
graph_bottom_left=(0, 0),
graph_top_right=(1800, 900),
key="-GRAPH-",
change_submits=True, # mouse click events
background_color='white',
drag_submits=True
)
]
]
return sg.Window('Image Controller Window', layout, size=(1920,1080), finalize=True)
# ------------------------------------------------------------------------------
window1, window2 = make_win1(), None # start off with 1 window open
while True: # Event Loop
window, event, values = sg.read_all_windows()
if event == sg.WIN_CLOSED or event == 'Exit':
window.close()
if window == window2: # if closing win 2, mark as closed
window2 = None
elif window == window1: # if closing win 1, exit program
break
elif event == "-FOLDER-":
folder = values["-FOLDER-"]
try:
# Get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
fnames = [f for f in file_list if os.path.isfile(os.path.join(folder, f)) and f.lower().endswith(img_types)]
window1["-FILE LIST-"].update(fnames)
elif event == "-FILE LIST-": # A file was chosen from the listbox
if not window2:
window2 = make_win2()
graph = window2.Element("-GRAPH-")
try:
filename = os.path.join(values["-FOLDER-"], values["-FILE LIST-"][0])
graph.DrawImage(data=filename, location=(0,540), size = (400,400))
except:
pass
window1.close()```
说法错误,
graph.draw_image(data=filename, location=(0,540), size = (400,400))
方法定义为
def draw_image(self, filename=None, data=None, location=(None, None)):
"""
:param filename: if image is in a file, path and filename for the image. (GIF and PNG only!)
"""
所以应该是
graph.draw_image(filename=filename, location=(0,540))
它只适用于 PNG 和 GIF 文件,您可以对不同类型的图像使用以下代码。
from io import BytesIO
from PIL import Image
im = Image.open(filanme)
with BytesIO() as output:
im.save(output, format="PNG")
data = output.getvalue()
graph.draw_image(data=data, location=(0,540))