使用Tkinter制作一个文件选择器GUI来显示输入和输出fi

2024-06-25 06:51:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用python3、opencv和tkinter来创建一个GUI,在这个GUI中,我可以上传一个图像,然后在一个面板中显示该图像,最终图像是与输入图像在同一文件夹中的所有图像的堆叠版本。函数的作用是:将图像堆叠在文件夹中。在

由于某些原因,当运行这个程序时,应该在panelA中的图像正在显示,而来自panelB的图像则不显示。在

如何同时显示输入和输出图像? 还有什么方法可以加速这个代码吗?在

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

Tags: andthetopath图像imagenonegui
2条回答

您的代码按预期工作,但是由于图像的大小,这两个图像可能都没有显示。我建议调整图像大小。在

edged = cv2.resize(edged, dsize=(500, 600), interpolation=cv2.INTER_CUBIC)

这两个图像可能无法显示的原因是由于图像的大小。我建议使用resize函数来确保不管输入图像的大小,图像都能显示出来。在

我提供了一个代码片段来说明它应该如何显示。在

        edged = cv2.resize(edged, dsize=(500, 600), interpolation=cv2.INTER_CUBIC)

相关问题 更多 >