如何在python opencv GUI中打印某些功能?
我想在python openCV中打印图像的平均值、高度和宽度。我使用了两个按钮(获取照片和分析图像)和不同的GUI,一个用于获取照片(如何在python opencv GUI中打印某些功能?,python,opencv,image-processing,Python,Opencv,Image Processing,我想在python openCV中打印图像的平均值、高度和宽度。我使用了两个按钮(获取照片和分析图像)和不同的GUI,一个用于获取照片(def openphoto():),另一个用于打印这些功能(def feature():)。但我犯了个错误 注意,完整的代码太长了,所以我用了部分代码 我已经在python openCV中尝试过了 import tkinter as tk from tkinter.filedialog import askopenfilename import shutil i
def openphoto():
),另一个用于打印这些功能(def feature():
)。但我犯了个错误
注意,完整的代码太长了,所以我用了部分代码
我已经在python openCV中尝试过了
import tkinter as tk
from tkinter.filedialog import askopenfilename
import shutil
import os
from PIL import Image, ImageTk
window = tk.Tk()
window.title("Dr. Papaya")
window.geometry("500x510")
window.configure(background ="lightgreen")
title = tk.Label(text="Click below to choose picture for testing disease....", background = "lightgreen", fg="Brown", font=("", 15))
title.grid()
def feature():
window.destroy()
window1 = tk.Tk()
window1.title(" ")
window1.geometry("650x510")
window1.configure(background="lightgreen")
def exit():
window1.destroy()
#i want to print here
print("Mean : ",mean)
print("Heigth : ",heigth)
print("Width : ",width)
button = tk.Button(text="Exit", command=exit)
button.grid(column=0, row=9, padx=20, pady=20)
window1.mainloop()
def openphoto():
import cv2
import numpy as np
fileList = os.listdir(dirPath)
for fileName in fileList:
os.remove(dirPath + "/" + fileName)
fileName = askopenfilename(initialdir='', title='Select image for analysis ',
filetypes=[('image files', '.jpg')])
dst = " "
shutil.copy(fileName, dst)
load = Image.open(fileName)
#calculate the mean
mean=np.mean(load)
#calculate the height & width
height = np.size(load, 0)
width = np.size(load, 1)
render = ImageTk.PhotoImage(load)
img = tk.Label(image=render, height="250", width="500")
img.image = render
img.place(x=0, y=0)
img.grid(column=0, row=1, padx=10, pady = 10)
title.destroy()
button1.destroy()
button2 = tk.Button(text="Analyse Image", command=feature)
button2.grid(column=0, row=2, padx=10, pady = 10)
button1 = tk.Button(text="Get Photo", command = openphoto)
button1.grid(column=0, row=1, padx=10, pady = 10)
window.mainloop()
当您尝试打印变量时,这些变量不在范围内。这是一个重要的编程原则,所以我建议你读一下 可以将变量设为全局变量,以使其在函数外部可访问:
def openphoto():
global width, height, mean
[rest of code]
您能在计算后立即打印这些值吗?这些值正在控制台中打印,但我想在GUI窗口中打印。我认为“打印”在这里不起作用。打印总是输出到控制台。要向UI添加文本,可以使用tkinter文本小部件。阅读thread以获取示例。