Python 使用Tkinter制作文件选择器GUI以显示输入和输出文件
我正在使用Python3、opencv和tkinter创建一个GUI,在其中我可以上载一个图像,然后在最终图像旁边的面板中显示该图像,该图像是与输入图像位于同一文件夹中的所有图像的堆叠版本。函数的作用是将图像堆叠到文件夹中 由于某些原因,当运行此程序时,应该在panelA中的图像正在显示,而panelB中的图像则没有显示 如何同时显示输入和输出图像? 还有什么方法可以加速这个代码Python 使用Tkinter制作文件选择器GUI以显示输入和输出文件,python,python-3.x,image,opencv,tkinter,Python,Python 3.x,Image,Opencv,Tkinter,我正在使用Python3、opencv和tkinter创建一个GUI,在其中我可以上载一个图像,然后在最终图像旁边的面板中显示该图像,该图像是与输入图像位于同一文件夹中的所有图像的堆叠版本。函数的作用是将图像堆叠到文件夹中 由于某些原因,当运行此程序时,应该在panelA中的图像正在显示,而panelB中的图像则没有显示 如何同时显示输入和输出图像? 还有什么方法可以加速这个代码 def select_image(): # grab a reference to the image panel
def select_image():
# grab a reference to the image panels
global panelA, panelB
# open a file chooser dialog and allow the user to select an input
# image
path = tkinter.filedialog.askopenfilename()
# ensure a file path was selected
if len(path) > 0:
# load the image from disk, convert it to grayscale, and detect
# edges in it
image = cv2.imread(path)
edged = stacker(os.path.dirname(os.path.dirname(path)))
# OpenCV represents images in BGR order; however PIL represents
# images in RGB order, so we need to swap the channels
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# convert the images to PIL format...
image = Image.fromarray(image)
edged = Image.fromarray(edged)
# ...and then to ImageTk format
image = ImageTk.PhotoImage(image)
edged = ImageTk.PhotoImage(edged)
# if the panels are None, initialize them
if panelA is None or panelB is None:
# the first panel will store our original image
panelA = tk.Label(image=image)
panelA.image = image
panelA.pack(side="left", padx=10, pady=10)
# while the second panel will store the edge map
panelB = tk.Label(image=edged)
panelB.image = edged
panelB.pack(side="right", padx=10, pady=10)
# otherwise, update the image panels
else:
# update the pannels
panelA.configure(image=image)
panelB.configure(image=edged)
panelA.image = image
panelB.image = edged
# initialize the window toolkit along with the two image panels
root = tk.Tk()
panelA = None
panelB = None
print("done")
# create a button, then when pressed, will trigger a file chooser
# dialog and allow the user to select an input image; then add the
# button the GUI
btn = tk.Button(root, text="Select an image", command=select_image)
print("done1")
btn.pack(side="bottom", fill="both", expand="yes", padx="10", pady="10")
print("done2")
# kick off the GUI
root.mainloop()
print("done3")
两个图像都无法显示的原因是图像的大小。我建议使用调整大小功能,以确保无论输入图像大小,图像都能显示 我在下面提供了一段代码片段,说明它应该是什么样子
edged = cv2.resize(edged, dsize=(500, 600), interpolation=cv2.INTER_CUBIC)
您的代码按预期工作,但由于图像的大小,这两个图像可能都无法显示。我建议在图像上调整大小
edged = cv2.resize(edged, dsize=(500, 600), interpolation=cv2.INTER_CUBIC)
我觉得你的代码还可以。显然,我无法测试它,因为您没有提供一个和一些测试图像。因此,我猜问题出在stacker()函数中;可能它返回了一个空数组?您是想使用
os.path.dirname
两次吗?这会把你放在父文件夹中,而不是“与输入的图像相同的文件夹”。@Novel谢谢你抓住了这个错误,我修复了它。此时不允许我提供stacker()函数,但如果我尝试在不同面板中显示输入图像两次,程序甚至无法工作。如果两个标签都使用“image”,则您的代码对我有效。向我们展示一个演示您的问题的示例。它是否显示图像两次?在左面板和右面板中@此外,您可能需要检查映像类型或numpy与python数组的兼容性。