-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
196 lines (183 loc) · 6.78 KB
/
search.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# Erfanullah Arsala
# 11/20/2019
# Abstract: A GUI that prompts a user to select a image filter option
# and a search bar to enter information about
# picture and once "Search" (thanks to cookie and Erik for helping me this part) button is clicked
# the code will search through a dictionary of images
# to find the image with the best matching tags
# FYI: All images have to be in same directory as python file
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QLabel, QPushButton,
QLineEdit, QHBoxLayout, QVBoxLayout, QDialog,
QTextBrowser, QComboBox)
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import pyqtSlot, QUrl
from PyQt5.QtGui import QColor
from PIL import Image
#dropdown part
url_list = [
'Pick a value',
'Sepia',
'Negative',
'Grayscale',
'Thumbnail',
'None'
]
#dictionary of images info
image_info = [
{
"id" : "34694102243_3370955cf9_z",
"title" : "Eastern",
"flickr_user" : "Sean Davis",
"tags" : ["Los Angeles", "California", "building"]
},
{
"id" : "37198655640_b64940bd52_z",
"title" : "Spreetunnel",
"flickr_user" : "Jens-Olaf Walter",
"tags" : ["Berlin", "Germany", "tunnel", "ceiling"]
},
{
"id" : "36909037971_884bd535b1_z",
"title" : "East Side Gallery",
"flickr_user" : "Pieter van der Velden",
"tags" : ["Berlin", "wall", "mosaic", "sky", "clouds"]
},
{
"id" : "36604481574_c9f5817172_z",
"title" : "Lombardia, september 2017",
"flickr_user" : "Mónica Pinheiro",
"tags" : ["Italy", "Lombardia", "alley", "building", "wall"]
},
{
"id" : "36885467710_124f3d1e5d_z",
"title" : "Palazzo Madama",
"flickr_user" : "Kevin Kimtis",
"tags" : [ "Rome", "Italy", "window", "road", "building"]
},
{
"id" : "37246779151_f26641d17f_z",
"title" : "Rijksmuseum library",
"flickr_user" : "John Keogh",
"tags" : ["Amsterdam", "Netherlands", "book", "library", "museum"]
},
{
"id" : "36523127054_763afc5ed0_z",
"title" : "Canoeing in Amsterdam",
"flickr_user" : "bdodane",
"tags" : ["Amsterdam", "Netherlands", "canal", "boat"]
},
{
"id" : "35889114281_85553fed76_z",
"title" : "Quiet at dawn, Cabo San Lucas",
"flickr_user" : "Erin Johnson",
"tags" : ["Mexico", "Cabo", "beach", "cactus", "sunrise"]
},
{
"id" : "34944112220_de5c2684e7_z",
"title" : "View from our rental",
"flickr_user" : "Doug Finney",
"tags" : ["Mexico", "ocean", "beach", "palm"]
},
{
"id" : "36140096743_df8ef41874_z",
"title" : "Someday",
"flickr_user" : "Thomas Hawk",
"tags" : ["Los Angeles", "Hollywood", "California", "Volkswagen", "Beatle", "car"]
}
]
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel("Find Image: ")
self.label2 = QLabel("Image Filter:")
self.line_edit = QLineEdit()
self.combo_box = QComboBox()
self.combo_box.addItems(url_list)
self.search = QPushButton("Search")
#apply vbox layout
vbox = QVBoxLayout()
#add "image filter"
vbox.addWidget(self.label2)
#add dropdown
vbox.addWidget(self.combo_box)
#add "find image"
vbox.addWidget(self.label)
#add search line
vbox.addWidget(self.line_edit)
#add "search" button
vbox.addWidget(self.search)
self.setLayout(vbox)
self.setWindowTitle("Homework 3: Image Search")
self.search.clicked.connect(self.new_win)
for i in range(10):
#convert titles and tags into lower case
image_info[i]["title"] = image_info[i]["title"].split()
image_info[i]["title"] = [x.lower() for x in image_info[i]["title"]]
image_info[i]["tags"] = [x.lower() for x in image_info[i]["tags"]]
@pyqtSlot()
def new_win(self):
self.combo_box.currentText()
text = self.line_edit.text()
count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
super().__init__()
text = text.split()
#change item to lowercase
text = [item.lower() for item in text]
print(text)
for word in text:
print(word)
for item in range(len(image_info)):
if(word in image_info[item]["tags"] or word in image_info[item]["title"]):
count[item] += 1
keeping_count = []
#get max numbers
for num in range(len(count)):
if(count[num] == max(count)):
keeping_count.append((image_info[num]["title"][0], num))
#sort the list after appending the tuple
sort = sorted(keeping_count)
print(sort)
index = (sort[0][1])
im = Image.open(image_info[index]["id"] + ".jpg")
#get index of what the person chooses on the list
i = self.combo_box.currentIndex()
if i!=0:
#sepia function to apply sepia filter to image
if i == 1:
newList = []
for pic in im.getdata():
temp = (pic[0], int(pic[1]*.7), int(pic[2]*.7))
newList.append(temp)
im.putdata(newList)
im.show()
#negative function to apply negative filter to image
elif i == 2:
picture = im
newList = []
for p in picture.getdata():
# reduce the green and blue channel by multiplying by .70 and getting integer value of that
temp = (p[0], int(p[1] * .20), int(p[2]* .60))
# overwrite list and add new values to the list
newList.append(temp)
# all data from picture is replaced with newList data
picture.putdata(newList)
# save the new picture with reduced green and blue channel values
picture.show()
# greyscale function to apply greyscale filter to image
elif i == 3:
image = im.convert('LA')
image.show()
# thumbnail function to apply thumbnail filter to image
elif i == 4:
size = (250, 250)
im.thumbnail(size)
im.show()
#if they choose none just open up the image
else:
im.show()
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())