-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlenna.py
176 lines (154 loc) · 7.6 KB
/
lenna.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
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'lenna.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog
from PyQt5.QtGui import QImage
import cv2
import imutils
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout_2.setObjectName("gridLayout_2")
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap("Lenna.png"))
self.label.setObjectName("label")
self.horizontalLayout_2.addWidget(self.label)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalSlider = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider.setObjectName("verticalSlider")
self.horizontalLayout.addWidget(self.verticalSlider)
self.verticalSlider_2 = QtWidgets.QSlider(self.centralwidget)
self.verticalSlider_2.setOrientation(QtCore.Qt.Vertical)
self.verticalSlider_2.setObjectName("verticalSlider_2")
self.horizontalLayout.addWidget(self.verticalSlider_2)
self.horizontalLayout_2.addLayout(self.horizontalLayout)
self.gridLayout.addLayout(self.horizontalLayout_2, 0, 0, 1, 3)
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 1, 0, 1, 1)
self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 1, 1, 1, 1)
spacerItem = QtWidgets.QSpacerItem(148, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
self.gridLayout.addItem(spacerItem, 1, 2, 1, 1)
self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.verticalSlider.valueChanged['int'].connect(self.brightness_value) # type: ignore
self.verticalSlider_2.valueChanged['int'].connect(self.blur_value) # type: ignore
self.pushButton.clicked.connect(self.loadImage) # type: ignore
self.pushButton_2.clicked.connect(self.savePhoto) # type: ignore
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# Added code here
self.filename = None # Will hold the image address location
self.tmp = None # Will hold the temporary image for display
self.brightness_value_now = 0 # Updated brightness value
self.blur_value_now = 0 # Updated blur value
def loadImage(self):
""" This function will load the user selected image
and set it to label using the setPhoto function
"""
self.filename = QFileDialog.getOpenFileName(filter="Image (*.*)")[0]
self.image = cv2.imread(self.filename)
self.setPhoto(self.image)
def setPhoto(self, image):
""" This function will take image input and resize it
only for display purpose and convert it to QImage
to set at the label.
"""
self.tmp = image
image = imutils.resize(image, width=640)
frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888)
self.label.setPixmap(QtGui.QPixmap.fromImage(image))
def brightness_value(self, value):
""" This function will take value from the slider
for the brightness from 0 to 99
"""
self.brightness_value_now = value
print('Brightness: ', value)
self.update()
def blur_value(self, value):
""" This function will take value from the slider
for the blur from 0 to 99 """
self.blur_value_now = value
print('Blur: ', value)
self.update()
def changeBrightness(self, img, value):
""" This function will take an image (img) and the brightness
value. It will perform the brightness change using OpenCv
and after split, will merge the img and return it.
"""
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
lim = 255 - value
v[v > lim] = 255
v[v <= lim] += value
final_hsv = cv2.merge((h, s, v))
img = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
return img
def changeBlur(self, img, value):
""" This function will take the img image and blur values as inputs.
After perform blur operation using opencv function, it returns
the image img.
"""
kernel_size = (value + 1, value + 1) # +1 is to avoid 0
img = cv2.blur(img, kernel_size)
return img
def update(self):
""" This function will update the photo according to the
current values of blur and brightness and set it to photo label.
"""
img = self.changeBrightness(self.image, self.brightness_value_now)
img = self.changeBlur(img, self.blur_value_now)
self.setPhoto(img)
def savePhoto(self):
""" This function will save the image"""
# here provide the output file name
# lets say we want to save the output as a time stamp
# uncomment the two lines below
# import time
# filename = 'Snapshot '+str(time.strftime("%Y-%b-%d at %H.%M.%S %p"))+'.png'
# Or we can give any name such as output.jpg or output.png as well
# filename = 'Snapshot.png'
# Or a much better option is to let user decide the location and the extension
# using a file dialog.
filename = QFileDialog.getSaveFileName(filter="JPG(*.jpg);;PNG(*.png);;TIFF(*.tiff);;BMP(*.bmp)")[0]
cv2.imwrite(filename, self.tmp)
print('Image saved as:', self.filename)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Open"))
self.pushButton_2.setText(_translate("MainWindow", "Save"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())