python打開圖片放到tkinter的界面里(opencv方式)
上次使用的別人的代碼打開圖片放到tkinter的圖型界面當(dāng)中。
雖然感覺沒有很懂但是不是自己想用的模塊。今天換成了opencv的方式,修改成功。
代碼如下:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar ?7 18:03:50 2020
@author: liyan
"""
import tkinter
from tkinter import *
from PIL import Image,ImageTk ?###這個是沒有想到的模塊,也不確定能不能省?
from tkinter.filedialog import askopenfilename
import time
import CV2
root = Tk()
root.geometry('500x500') ?##這個小了一點,不知道怎么自適應(yīng)
root.title('圖片處理')
def choosepic():
? ? path_ = askopenfilename()
? ? img= CV2.imread(path_ )
? ? current_image = Image.fromarray(img)
? ? imgtk = ImageTk.PhotoImage(image=current_image)
? ? image_label.config(image=imgtk)
? ? image_label.image = imgtk # keep a reference
path = StringVar()
Button(root, text='選擇圖片', command=choosepic).pack()
file_entry = Entry(root, state='readonly', text=path)
#file_entry.pack()?
image_label = Label(root)
image_label.pack()
root.mainloop()
完成圖形界面并且可以很好的打開并顯示。方便下一步處理圖像里面的信息。