为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕码人2483693 2021-04-02 17:44:10
我正在使用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 panelsroot = tk.Tk()panelA = NonepanelB = Noneprint("done")
查看完整描述

2 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

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

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


查看完整回答
反对 回复 2021-04-27
?
阿晨1998

TA贡献2037条经验 获得超6个赞

可能无法显示两个图像的原因是图像的尺寸。我建议使用调整大小功能以确保无论输入图像大小如何都显示图像。

我提供了下面的代码片段。

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


查看完整回答
反对 回复 2021-04-27
  • 2 回答
  • 0 关注
  • 519 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号