[英]how do I display in Tkinter canvas
I'm new to python GUI and I'm going to display my result but I have some problem with displaying it.我是 python GUI 的新手,我要显示我的结果,但我在显示它时遇到了一些问题。 I put the whole code but I have a problem with the
def blur(file_path)
function.我放了整个代码,但我对
def blur(file_path)
函数有问题。 here is my code :这是我的代码:
from tkinter import *
from PIL import ImageTk, Image
from tkinter import filedialog
import cv2 as cv
# =================================== statics and configuration ===================================
color = '#20536C'
root = Tk()
root.title('Opticdisk and Macula detector')
root.configure(bg= color)
root.geometry('1070x700')
root.resizable(width=False, height=False)
root.iconbitmap('J:\Projects\Bachelor Project\download.ico')
filename_path = {}
# =================================== Frames ===================================
top = Frame(root, width=1070, height=70,pady = 9, bg=color)
top.pack(side=TOP)
# top.grid(row = 0 , column= 1)
left = Frame(root, width=750, height=630, bg=color)
left.pack(side=LEFT)
# left.grid(row = 1 , column= 1)
right = Frame(root, width=320, height=630, bg="red")
right.pack(side=LEFT)
# =================================== functions and body ===================================
img1 = ImageTk.PhotoImage(Image.open('J:/Projects/Bachelor Project/eye.ico'))
def open_image(file_path):
file_path['image'] = filedialog.askopenfilename(initialdir="J://uni//final project//Data set",
title="select an image",
filetypes=(('all files', '*.*'), ('jpg files', '*.jpg'), ('tif file','*.tif')))
mainImage = ImageTk.PhotoImage(Image.open(filename_path['image']))
lbl = Label(left, image=mainImage,
width= 749,
height=630,
bg='#020101')#.place(x=20, y=0)
lbl.image = mainImage # keep a reference! to show the image
lbl.place(x=0, y=0)
def blur(file_path):
# messagebox = Message(left).place(x=20,y=10)
try:
Im = cv.imread(file_path['image'])
I = cv.medianBlur(Im,15)
I = cv.resize(I, (300, 300))
canvas = Canvas(left, width=749, height=630)
# canvas.place(x=0, y=0)
# canvas.pack()
# canvas.create_image(20, 20, anchor=NW, image=I)
# canvas.image = I
canvas.pack()
cv.imshow('result', I)
cv.waitKey()
except:
print('error')
# =================================== Buttons ===================================
btnBrowse = Button(top, width=93,
text='select file',
fg='#58859a',
font=('Times', 15, 'italic', 'bold'),
bg='#03283a',
command = lambda :open_image(filename_path))
btnBrowse.pack(side=BOTTOM)
btnMask = Button(right, text='Opticdisk',
fg= '#58859a',
font=('Times', 20, 'italic', 'bold'),
bg="#03283a",
width=19,
height=6,
command=lambda: blur(filename_path))
btnMask.pack(side=TOP)
btnMakula = Button(right, text='Makula',
fg= '#58859a',
font=('Times', 20, 'italic', 'bold'),
bg="#03283a",
width=19,
height=6)
btnMakula.pack(side=TOP)
btnClear = Button(right, text='exit',
fg= '#58859a',
font=('Times', 20, 'italic', 'bold'),
bg="#03283a",
width=19,
height=6,
command=root.quit)
btnClear.pack(side=TOP)
root.mainloop()
I'm going to display I
.我要显示
I
。 As you can see in the comments I try canvas
but it shows nothing but a white screen正如您在评论中看到的,我尝试使用
canvas
但它只显示一个白屏
OpenCV library shows the picture I
with no problem as I want in the line cv.imshow('result', I)
but I want to display it inside the program. OpenCV 库在
cv.imshow('result', I)
行中显示I
没有问题的图片cv.imshow('result', I)
但我想在程序中显示它。
I would be appreciated if you guys help me?如果你们帮助我,我将不胜感激?
as I'm in a hurry I found a solution for my above problem I just save the resulting photo I
and then put the address of the file in a dictionary path['image']
and open it with the same way we open folders in function open_image(file_path)
here is my solution :因为我很着急,所以我找到了解决上述问题的方法,我只是保存生成的照片
I
,然后将文件的地址放在字典path['image']
,并使用与打开文件夹相同的方式打开它函数open_image(file_path)
这是我的解决方案:
def blur(file_path):
path={}
# messagebox = Message(left).place(x=20,y=10)
try:
Im = cv.imread(file_path['image'])
I = cv.medianBlur(Im,15)
I = cv.resize(I, (300, 300))
# here is the solution
cv.imwrite('J://uni//final project//res_image//finalresult.jpg',I)
path['image'] = ('J://uni//final project//res_image//finalresult.jpg')
mainImage = ImageTk.PhotoImage(Image.open(path['image']))
lbl = Label(left, image=mainImage,
width=749,
height=630,
bg='#020101') # .place(x=20, y=0)
lbl.image = mainImage # keep a reference! to show the image
lbl.place(x=0, y=0)
#cv.imshow('result', I)
#cv.waitKey()
except:
print('error')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.