如何从python中的图像在GUI中打印值?

时间:2019-01-30 19:00:21

标签: python image opencv image-processing gui-builder

我是GUI开发的新手。在这里,我创建了两个GUI,一个用于拍照,另一个用于显示功能。所以,我使用了两个功能。但是我不知道一些事情。现在我需要两种帮助。 1)在GUI(不是在控制台上)上打印浮点值的命令是什么? 2)如何计算均值,方差,s.d。等等,以及如何将这些值从一个函数传递到另一个函数?

ast.literal_eval

1 个答案:

答案 0 :(得分:0)

好的,我花了一些时间研究它。
关于值的计算,您先前的question可以解决此问题,但是需要全局访问变量。关于文本的显示,您必须添加一个tkinter文本小部件。如果您想添加更多计算值,只需在google上输入numpy +“根据需要获得价值”即可。

我已经采用了您的代码并创建了一个有效的示例,请参见下面的代码。请注意,我删除了该示例不需要的一些内容,因此将所需的行复制到自己的代码中。另外,请检出this reference的文本小部件。

结果:

enter image description here

代码:
注意:我特意创建了2个文本小部件,以显示实现多种文本的2种方式

import tkinter as tk
from tkinter.filedialog import askopenfilename
import shutil
import os
from PIL import Image, ImageTk

window = tk.Tk()

window.title(" ")

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(" ")

    ### create a text widget, place it in window1 and insert the text
    width_txt = tk.Text(window1, height=2, width=30, fg="RED", background = "lightgreen", relief="flat")
    width_txt.grid(column=0, row=0)
    width_txt.insert(tk.END, "Width: " + str(width))

    height_txt = tk.Text(window1, height=2, width=30, fg="RED", background = "lightgreen", relief="flat")
    height_txt.grid(column=0, row=1)
    height_txt.insert(tk.END, "Height: " + str(height) + "\nMean: " + str(mean))


    window1.geometry("650x510")
    window1.configure(background="lightgreen")

def openphoto():

    ### this line makes the variables accessible everywhere
    global width,height, mean
    import numpy as np

    fileName = askopenfilename(initialdir='', title='Select image for analysis ',
                filetypes=[('image files', '.jpg')])
    photo = Image.open(fileName)

    #### calculate values 
    height = np.size(photo, 0)
    width = np.size(photo, 1)
    mean = np.mean(photo)


    render = ImageTk.PhotoImage(photo)
    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()