Image Processing Application Using Python

Input Image

Python Code (imageProcessingPythonGUI.py)

from tkinter import *

from tkinter.messagebox import showerror

import webbrowser

def close():

main.destroy()

def helpurl():

url = 'sites.google.com/view/vsat2k'

webbrowser.open_new(url)

def negative():

num = r.get()

num = int(num)

result.set(num)

print('Radio Selected: ', num)

myGrayscale = im.convert('L')

myGrayscale.save('satish_greyscale.jpg','JPEG')

img1 = ImageTk.PhotoImage(file='satish_greyscale.jpg')

panel3 = Label(main, image = img1)

panel3.img1 = img1

panel3.grid(row=1,column=1)

panel4 = Label(main, text = 'Gray Scale Image').grid(row=2,column=1)

#myNegative = im.getpixel()

myBinary = ImageOps.invert(myGrayscale)

myBinary.save('satish_greyscale_bin.jpg','JPEG')

#myGrayscaleTh = myGrayscale.point(lambda p: p > num and 255)

#myGrayscaleTh.save('satish_greyscale_th.jpg','JPEG')

img3 = ImageTk.PhotoImage(file='satish_greyscale_bin.jpg')

panel4 = Label(main, image = img3)

panel4.img3 = img3

panel4.grid(row=1,column=3)

panel5 = Label(main, text = 'Image Negative').grid(row=2,column=3)

outTh = Label(main, textvariable=result).grid(row=3,column=2)

return

def tune():

selection = "Scale Value = " + str(var.get())

scaleLabel.config(text = selection)

print('Threshold Value (Entered by User): ', selection)

num = var.get()

num = int(num)

result.set(num)

myGrayscale = im.convert('L')

myGrayscale.save('satish_greyscale.jpg','JPEG')

img1 = ImageTk.PhotoImage(file='satish_greyscale.jpg')

panel3 = Label(main, image = img1)

panel3.img1 = img1

panel3.grid(row=1,column=1)

panel4 = Label(main, text = 'Gray Scale Image').grid(row=2,column=1)

print('Threshold Value (Tuned by User): ', num)

myGrayscaleTh = myGrayscale.point(lambda p: p > num and 255)

myGrayscaleTh.save('satish_greyscale_th.jpg','JPEG')

img2 = ImageTk.PhotoImage(file='satish_greyscale_th.jpg')

panel4 = Label(main, image = img2)

panel4.img2 = img2

panel4.grid(row=1,column=2)

panel5 = Label(main, text = 'Thresholded Image').grid(row=2,column=2)

outTh = Label(main, textvariable=result).grid(row=3,column=2)

return

def radio():

num = r.get()

num = int(num)

result.set(num)

print('Radio Selected: ', num)

myGrayscale = im.convert('L')

myGrayscale.save('satish_greyscale.jpg','JPEG')

img1 = ImageTk.PhotoImage(file='satish_greyscale.jpg')

panel3 = Label(main, image = img1)

panel3.img1 = img1

panel3.grid(row=1,column=1)

panel4 = Label(main, text = 'Gray Scale Image').grid(row=2,column=1)

print('Threshold Value (Radio Selected by User): ', num)

myGrayscaleTh = myGrayscale.point(lambda p: p > num and 255)

myGrayscaleTh.save('satish_greyscale_th.jpg','JPEG')

img2 = ImageTk.PhotoImage(file='satish_greyscale_th.jpg')

panel4 = Label(main, image = img2)

panel4.img2 = img2

panel4.grid(row=1,column=2)

panel5 = Label(main, text = 'Thresholded Image').grid(row=2,column=2)

outTh = Label(main, textvariable=result).grid(row=3,column=2)

return

def menu():

num = op.get()

num = int(num)

result.set(num)

print('Menu Selected: ', num)

myGrayscale = im.convert('L')

myGrayscale.save('satish_greyscale.jpg','JPEG')

img1 = ImageTk.PhotoImage(file='satish_greyscale.jpg')

panel3 = Label(main, image = img1)

panel3.img1 = img1

panel3.grid(row=1,column=1)

panel4 = Label(main, text = 'Gray Scale Image').grid(row=2,column=1)

print('Threshold Value (Menu Opted by User): ', num)

myGrayscaleTh = myGrayscale.point(lambda p: p > num and 255)

myGrayscaleTh.save('satish_greyscale_th.jpg','JPEG')

img2 = ImageTk.PhotoImage(file='satish_greyscale_th.jpg')

panel4 = Label(main, image = img2)

panel4.img2 = img2

panel4.grid(row=1,column=2)

panel5 = Label(main, text = 'Thresholded Image').grid(row=2,column=2)

outTh = Label(main, textvariable=result).grid(row=3,column=2)

return

def perform():

num = nameentry.get()

try:

num = int(num)

if num > 255:

showerror('Out-of Range', 'Please enter an integer between 0 - 255')

except ValueError:

#Display Error Window (Title, Prompt)

showerror('Non-Int Error', 'Please enter an integer')

else:

result.set(num)

myGrayscale = im.convert('L')

myGrayscale.save('satish_greyscale.jpg','JPEG')

img1 = ImageTk.PhotoImage(file='satish_greyscale.jpg')

panel3 = Label(main, image = img1)

panel3.img1 = img1

panel3.grid(row=1,column=1)

panel4 = Label(main, text = 'Gray Scale Image').grid(row=2,column=1)

print('Threshold Value (Entered by User): ', num)

myGrayscaleTh = myGrayscale.point(lambda p: p > num and 255)

myGrayscaleTh.save('satish_greyscale_th.jpg','JPEG')

img2 = ImageTk.PhotoImage(file='satish_greyscale_th.jpg')

panel4 = Label(main, image = img2)

panel4.img2 = img2

panel4.grid(row=1,column=2)

panel5 = Label(main, text = 'Thresholded Image').grid(row=2,column=2)

outTh = Label(main, textvariable=result).grid(row=3,column=2)

return

main = Tk()

main.geometry('700x450+350+150')

main.title('Image Thresholding')

from PIL import Image, ImageTk, ImageOps

#im = Image.open('satish.png')

fp = open("satish.png", "rb")

im = Image.open(fp)

img = ImageTk.PhotoImage(im)

panel1 = Label(main, image = img).grid(row=1,column=0)

panel2 = Label(main, text = 'Input Image').grid(row=2,column=0)

print('Image Format: ', im.format)

print("Size of Image WxH: %dx%d" % im.size)

print('Color Model: ', im.mode)

nameentry = StringVar()

result = StringVar()

th = Label(main, text="Enter Threshold").grid(row=0,column=0)

inputTh = Entry(main, textvariable=nameentry).grid(row=0,column=1)

nameentry.set(100)

num = nameentry.get()

print('Threshold Value (Default): ', num)

button1 = Button(main, text='Perform Thresholding', command = perform)

button1.grid(row=0,column=2)

#var = DoubleVar()

var = StringVar()

scale = Scale(main, variable = var,fg='red',from_=0,to=255,orient=HORIZONTAL).grid(row=4,column=2)

scaleButton = Button(main, text="Get Scale Value", command = tune)

scaleButton.grid(row=6,column=2)

scaleLabel = Label(main, fg='blue')

scaleLabel.grid(row=5,column=2)

#Radiobutton

r = IntVar()

r1 = Radiobutton(main,text='100',variable=r,value=100,command=radio).grid(row=3,column=0)

r2 = Radiobutton(main,text='110',variable=r,value=110,command=radio).grid(row=4,column=0)

r3 = Radiobutton(main,text='120',variable=r,value=120,command=radio).grid(row=5,column=0)

r4 = Radiobutton(main,text='130',variable=r,value=130,command=radio).grid(row=6,column=0)

#OptionMenu

op = StringVar(main)

op.set('Select Th...') #default value

optMenu = OptionMenu(main, op,'80','90','100','110','120','130').grid(row=3,column=1)

optButton = Button(main, text="Get Option Value", command=menu)

optButton.grid(row=4,column=1)

#Image Negative

imNegativeButton = Button(main, text="Image Negataive", command=negative)

imNegativeButton.grid(row=3,column=3)

#Help

#url = 'sites.google.com/view/vsat2k'

#helpButton = Button(main, text="Help", command=helpurl(url), fg='green', width=10)

helpButton = Button(main, text="Help", command=helpurl, fg='green', width=10)

helpButton.grid(row=7,column=4)

#Close

closeButton = Button(main, command=close, text="Close", width=10)

closeButton.grid(row=7,column=7)

main.mainloop()

# End of File

#########################################