微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在Tkinter画布中显示

如何解决如何在Tkinter画布中显示

我是python GUI的新手,我将显示我的结果,但是在显示时遇到一些问题。 我输入了全部代码,但是def blur(file_path)函数有问题。这是我的代码

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,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,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',bg="#03283a",width=19,height=6,command=lambda: blur(filename_path))
btnMask.pack(side=TOP)

btnMakula = Button(right,text='Makula',height=6)
btnMakula.pack(side=TOP)

btnClear = Button(right,text='exit',command=root.quit)
btnClear.pack(side=TOP)
root.mainloop()

我将显示I。如您在评论中看到的,我尝试canvas,但它只显示白屏

OpenCV库在I行中显示图片cv.imshow('result',I)没有问题,但是我希望在程序内部显示它。

如果你们能帮助我,我将不胜感激?

解决方法

我很着急,找到了解决上述问题的方法 我只是保存生成的照片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')

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。