使用Tkinter制作文件选择器GUI以同时显示输入和输出文件

我正在使用python 3,opencv和tkinter创建一个GUI,可以在其中上传图像,然后在最终图像旁边的面板中显示该图像,该图像是与输入图像相同文件夹中所有图像的堆叠版本。stacker()函数只是将图像堆叠在文件夹中。


由于某些原因,在运行该程序时,将显示应该在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")

慕码人2483693
浏览 600回答 2
2回答

ITMISS

您的代码按预期工作,但是由于图像的大小,两个图像都可能不显示。我建议在图像上调整大小。edged = cv2.resize(edged, dsize=(500, 600), interpolation=cv2.INTER_CUBIC)

阿晨1998

可能无法显示两个图像的原因是图像的尺寸。我建议使用调整大小功能以确保无论输入图像大小如何都显示图像。我提供了下面的代码片段。        edged = cv2.resize(edged, dsize=(500, 600), interpolation=cv2.INTER_CUBIC)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python