-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFullImageApp.py
More file actions
140 lines (100 loc) · 3.69 KB
/
Copy pathFullImageApp.py
File metadata and controls
140 lines (100 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import os, os.path
from tkinter import *
from PIL import ImageTk, Image
import numpy as np
#Getting list of images in images directory
def getListOfImages():
imgs = []
path = os.getcwd() + "\images"
print(path)
valid_images = [".jpg",".jpeg",".png"]
for f in os.listdir(path):
ext = os.path.splitext(f)[1]
if ext.lower() not in valid_images:
continue
imgs.append(f)
return imgs
imgs = getListOfImages()
originalImagePath = ""
#Tkinter stuff
root = Tk()
root.title("My program")
root.geometry("800x800")
lb = Listbox(root, width = 200, selectmode = SINGLE)
for i in range(len(imgs)):
lb.insert(i, imgs[i])
lb.grid(row = 0, columnspan = 6)
#functions that are called when a particular button is clicked
def clickedOriginal():
img = Image.open(originalImagePath)
img.thumbnail((400, 400))
originalImg = ImageTk.PhotoImage(img)
myLabel.config(image = originalImg)
myLabel.image = originalImg
def clickedLeft():
img = Image.open(originalImagePath)
img.thumbnail((400, 400))
img = np.asarray(img)
#img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
numRows, numCols, colors = img.shape
halfPoint = numCols//2
imgLeft = Image.fromarray(img[:,0:halfPoint])
leftImg = ImageTk.PhotoImage(imgLeft)
myLabel.config(image = leftImg)
myLabel.image = leftImg
def clickedRight():
img = Image.open(originalImagePath)
img.thumbnail((400,400))
#img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
img = np.asarray(img)
numRows, numCols, colors = img.shape
halfPoint = numCols//2
imgRight = Image.fromarray(img[:, halfPoint:])
rightImg = ImageTk.PhotoImage(imgRight)
myLabel.config(image = rightImg)
myLabel.image = rightImg
def invertImage():
img = Image.open(originalImagePath)
img.thumbnail((400, 400))
img = np.asarray(img)
numRows, numCols, colors = img.shape
for row in range(0, numRows):
for col in range(0, numCols):
img[row][col][0] = 255 - img[row][col][0]
img[row][col][1] = 255 - img[row][col][1]
img[row][col][2] = 255 - img[row][col][2]
invertedImg = Image.fromarray(img)
invertedImg = ImageTk.PhotoImage(invertedImg)
myLabel.config(image = invertedImg)
myLabel.image = invertedImg
def removeRed():
img = Image.open(originalImagePath)
img.thumbnail((400, 400))
img = np.asarray(img)
numRows, numCols, colors = img.shape
for row in range(0, numRows):
for col in range(0, numCols):
img[row][col][0] = 0 #Make the red pixel value 0 for all pixels
redImg = Image.fromarray(img)
redImg = ImageTk.PhotoImage(redImg)
myLabel.config(image = redImg)
myLabel.image = redImg
def selectItem():
global originalImagePath
originalImagePath = "images\\" + lb.get(ANCHOR)
#Creating the buttons
buttonOriginal = Button(root,text = "See original image",command = clickedOriginal)
buttonOriginal.grid(row = 1, column = 1)
buttonLeft = Button(root,text = "See left half of image", command = clickedLeft)
buttonLeft.grid(row = 1, column = 2)
buttonRight = Button(root,text = "See right half of image", command = clickedRight)
buttonRight.grid(row = 1, column = 3)
buttonInvert = Button(root,text = "See inverted version of image", command = invertImage)
buttonInvert.grid(row = 1, column = 4)
buttonRemoveRed = Button(root,text = "Remove red colour from image", command = removeRed)
buttonRemoveRed.grid(row = 1, column = 5)
btnSelect = Button(root, text='Select image', command = selectItem)
btnSelect.grid(row = 1, column = 0)
myLabel = Label(root)
myLabel.grid(row = 2, columnspan = 6)
root.mainloop() #Main event loop