pythonimageopencvimage-processinggui-builder

How to print values in GUI from image in python?


I'm new in GUI developing.Here i'hv created two GUI, one for taking photo and another for showing features.so,i'hv used two functions.but i don't know some things.Now i need two kinds of help from you. 1)what is the command for printing float value in GUI(not on console)? 2)How to calculate the value of mean,variance ,s.d. etc from a image and how to pass those values from one function to another function?

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

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

      def exit():
             window1.destroy()
    #i want to print some features of image e.g. Mean, variance,s.d. Etc.
      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

    dirPath = " "
    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)
          #this is the image
          Photo = Image.open(fileName)

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

Solution

  • Okay, I took some more time to look into it.
    Concerning the calculation of the values, your previous question did that correct, however the variables needed to be accessible globally. Concerning the displaying of a text, you have to add a tkinter text widget. If you want to add more calculated values, just google for numpy + 'value your want'.

    I've taken your code and created a working example, see the code below. Note that I removed some stuff that wasn't neede for the example, so copy the lines you need to your own code. Also check out this reference for the text widget.

    Result:

    enter image description here

    Code:
    Note: I created 2 text widgets deliberately, to show 2 ways of implementing multiple texts

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