This is a simple application to make a browse button to allow the user to choose an image and then show it, but when I tried to use the variable img
or path
(when defined as global) the following error shows up:
cv2.imshow(" ",img)
NameError: name 'img' is not defined
Code:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
from cv2 import *
root=Tk()
root.title("filter applicator")
def open():
global my_image
root.filename=filedialog.askopenfilename(
initialdir="/", title="select a file",
filetypes=(("png files","*.png"),("allfiles","*.*")))
lbl=Label(root,text=root.filename).pack()
my_image=ImageTk.PhotoImage(Image.open(root.filename))
myimagelbl=Label(image=my_image).pack()
#global path
path=root.filename
global img
img=cv2.imread(path)
mybtn=Button(root,text="browse",command=open).pack()
cv2.imshow(" ",img)
cv2.waitKey(0)
cv2.destroyAllWindows()
root.mainloop()