2

I want to print the mean, height & width of an image in python openCV. Where i used two button (get photo and analysis image) and different GUI,one for getting the photo(def openphoto(): ) and another for printing those features(def feature(): ). But I'm getting error.

N.B. full code is too long.so, i used some part of it.

I've tried it in 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()
Nitin Prakash
  • 328
  • 3
  • 15
userTG
  • 43
  • 1
  • 1
  • 5

1 Answers1

0

The variables are not in scope when you try to print them. This is an important programming principle so I suggest you read this introduction

You can make the variable global to make the them accessible outside of the function:

def openphoto():
    global width, height, mean
    [rest of code]
J.D.
  • 4,511
  • 2
  • 7
  • 20
  • Can you print the values right after your calculated them? – J.D. Jan 30 '19 at 12:01
  • The values are printing in console but I want to print in the GUI windows.I think " print " is not working here.. – userTG Jan 30 '19 at 14:32
  • Print always outputs to the console. To add a text to the UI you can use a tkinter Text widget. Read [this](https://www.raspberrypi.org/forums/viewtopic.php?t=176943) thread for an example. – J.D. Jan 30 '19 at 19:30