我正在使用 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")
# 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)
关于python - 使用 Tkinter 制作文件选择器 GUI 以显示输入和输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51254118/