-
Notifications
You must be signed in to change notification settings - Fork 2
/
timelapse_gui.py
265 lines (230 loc) · 10.2 KB
/
timelapse_gui.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import sys
import os
import imghdr
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction
from PyQt5.QtWidgets import QWidget, QDesktopWidget, QMessageBox
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QGridLayout
from PyQt5.QtWidgets import QGroupBox, QPushButton, QSlider
from PyQt5.QtWidgets import QLabel, QFileDialog, QSizePolicy, QSpacerItem
from PyQt5.QtGui import QIcon, QDrag, QPixmap, QImage
from PyQt5.QtCore import Qt, pyqtSignal
from timelapse_processing import ImageList, Image, loadImage, toRGB
"""
QApplication: manages application object.
QWidget: base class of all user interface objects. Receives events from the window system.
QMainWindow: main application window - framework to build the apps' user interface.
QDesktopWidget: provides access to user screen information.
"""
class DropButton(QPushButton):
"""
Drag n Drop area widget
"""
itemDropped = pyqtSignal(list)
def __init__(self, title, width, height, parent):
super().__init__(title, parent)
self.setVisible(True)
self.setAcceptDrops(True)
self.setStyleSheet("background-color: rgba(85, 153, 255, 10%);"
"border-style: dashed;"
"border-width: 1px;"
"border-color: gray;"
"color: gray;")
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.setMinimumSize(width, height)
def dragEnterEvent(self, e):
m = e.mimeData()
if m.hasUrls():
e.accept()
else:
e.ignore()
def dropEvent(self, e):
m = e.mimeData()
if m.hasUrls():
e.accept()
links = []
[links.append(u.toLocalFile()) for u in m.urls()]
self.itemDropped.emit(links)
self.setVisible(False)
else:
e.ignore()
class TimelapseApp(QMainWindow):
"""
Timelapse exposure fix application window
"""
def __init__(self):
super().__init__()
self.title = 'Timelapse Exposure Fix'
self.icon = 'camera_icon.png'
self.left = 100
self.top = 100
self.width = 640
self.height = 480
self.minSize = 100
self.imgScale = 0.45
self.origImages = ImageList()
self.processedImages = ImageList()
self.imageFormat = ''
self.initUI()
def initUI(self):
# Menu bar
mainMenu = self.menuBar()
self.fileMenu = mainMenu.addMenu('File')
reloadAct = QAction('New Session', self)
reloadAct.setShortcut('Ctrl+R')
reloadAct.triggered.connect(self.reloadSession)
reloadAct.setStatusTip('Reload a new session for timelapse processing')
self.fileMenu.addAction(reloadAct)
saveAct = QAction('Save Images', self)
saveAct.setShortcut('Ctrl+S')
saveAct.triggered.connect(self.saveImages)
saveAct.setStatusTip('Save processed images')
self.fileMenu.addAction(saveAct)
saveAct.setDisabled(True)
exitAct = QAction('Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.triggered.connect(self.close)
exitAct.setStatusTip('Exit timelapse processing tool')
self.fileMenu.addAction(exitAct)
helpMenu = mainMenu.addMenu('Help')
aboutAct = QAction('Drag n Drop', self)
aboutAct.triggered.connect(self.helpWindow)
aboutAct.setStatusTip('Drag n Drop information')
helpMenu.addAction(aboutAct)
# Grid layout
self.createGridLayout()
self.dragndrop.itemDropped.connect(self.pictureDropped)
# Status bar
self.statusBar()
self.statusBar().setStyleSheet("background-color: white;")
# Window settings
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowIcon(QIcon(self.icon))
self.center()
self.show()
def createGridLayout(self):
self.centralWidget = QWidget(self)
self.centralWidget.setStyleSheet("background-color: white;")
self.centralWidget.setStatusTip('Ready')
self.setCentralWidget(self.centralWidget)
self.sld = QSlider(Qt.Horizontal, self)
mainLayout = QHBoxLayout()
vLayout = QVBoxLayout()
grid = QGridLayout()
grid.setSpacing(25)
grid.setContentsMargins(25, 25, 25, 25)
self.dragndrop = DropButton('Drop images here', self.width - self.minSize, self.minSize, self)
grid.addWidget(self.dragndrop, 0, 0, 1, 4)
self.viewerGroupBox = QGroupBox('Time-lapse viewer')
self.img1 = QLabel(self.centralWidget)
self.img1.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.img1.setMinimumSize(self.minSize, self.minSize)
self.img2 = QLabel(self.centralWidget)
self.img2.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.img2.setMinimumSize(self.minSize, self.minSize)
imgviewer = QHBoxLayout()
leftSpacer = QSpacerItem(self.minSize/10, self.minSize, QSizePolicy.Expanding, QSizePolicy.Minimum)
imgviewer.addSpacerItem(leftSpacer)
imgviewer.addWidget(self.img1)
imgviewer.addWidget(self.img2)
rightSpacer = QSpacerItem(self.minSize/10, self.minSize, QSizePolicy.Expanding, QSizePolicy.Minimum)
imgviewer.addSpacerItem(rightSpacer)
self.viewerGroupBox.setLayout(imgviewer)
self.viewerGroupBox.setVisible(False)
grid.addWidget(self.viewerGroupBox, 2, 1)
grid.addWidget(self.sld, 3, 1)
self.sld.setValue(0)
self.sld.setRange(0,0)
self.sld.valueChanged.connect(self.updateViewerIndex)
self.sld.setVisible(False)
# Encapsulate grid layout in VBox and HBox
vLayout.addLayout(grid)
verticalSpacer = QSpacerItem(self.minSize, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
vLayout.addItem(verticalSpacer)
hLeftSpacer = QSpacerItem(self.minSize/10, self.minSize, QSizePolicy.Expanding, QSizePolicy.Minimum)
mainLayout.addItem(hLeftSpacer)
mainLayout.addLayout(vLayout)
hRightSpacer = QSpacerItem(self.minSize/10, self.minSize, QSizePolicy.Expanding, QSizePolicy.Minimum)
mainLayout.addItem(hRightSpacer)
self.centralWidget.setLayout(mainLayout)
def updateViewerIndex(self):
self.updateViewer(self.sld.value())
def updateViewer(self, imageNumber):
if len(self.origImages) > 0:
rgb = toRGB(self.origImages[imageNumber].img)
qimage = QImage(rgb, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
pixmap1 = QPixmap(qimage)
pixmap1 = pixmap1.scaledToWidth(self.width * self.imgScale)
self.img1.setPixmap(pixmap1)
if len(self.processedImages) > 0:
rgb = toRGB(self.processedImages[imageNumber].img)
qimage = QImage(rgb, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
pixmap2 = QPixmap(qimage)
pixmap2 = pixmap2.scaledToWidth(self.width * self.imgScale)
self.img2.setPixmap(pixmap2)
def pictureDropped(self, links):
self.statusBar().showMessage('Processing...')
self.imageFormat = imghdr.what(links[0])
[self.origImages.append(Image(loadImage(link))) for link in links]
newImages = ImageList(self.origImages[:])
newImages.computeStats()
newImages.fixExposure()
self.processedImages = newImages
self.fileMenu.actions()[1].setDisabled(False)
self.updateViewer(0)
self.sld.setRange(0, len(self.origImages) - 1)
self.sld.setValue(0)
self.statusBar().showMessage('Ready')
self.viewerGroupBox.setVisible(True)
self.sld.setVisible(True)
def saveImages(self):
self.statusBar().showMessage('Saving Images...')
newDir = '/processed-images'
destDir = QFileDialog.getExistingDirectory(self, "Select Directory") + newDir
if not os.path.exists(destDir):
os.makedirs(destDir)
for i,obj in enumerate(self.processedImages):
rgb = toRGB(obj.img)
qimage = QImage(rgb, rgb.shape[1], rgb.shape[0], QImage.Format_RGB888)
qimage.save(destDir + '/processed_image' + str(i+1).zfill(4) + '.' + self.imageFormat)
self.statusBar().showMessage('Ready')
def reloadSession(self):
mboxtitle = 'Warning'
mboxmsg = 'Are you sure you want to start a new session?\nAll unsaved changes will be lost.'
reply = QMessageBox.warning(self, mboxtitle, mboxmsg,
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
del self.origImages[:]
del self.processedImages[:]
self.img1.clear()
self.img2.clear()
self.sld.setValue(0)
self.sld.setRange(0,0)
self.dragndrop.setVisible(True)
self.viewerGroupBox.setVisible(False)
self.sld.setVisible(False)
def helpWindow(self):
mboxtitle = 'Help'
mboxmsg = ('If your time-lapse is not displaying in the proper order, or there seems to be a jump '
'cut to previous frames, make sure the last item clicked when the images were dragged was '
'the first image of your sequence. The order of selection is preserved during drag and drop.')
reply = QMessageBox.information(self, mboxtitle, mboxmsg,
QMessageBox.Ok, QMessageBox.Ok)
def center(self):
qtRectangle = self.frameGeometry()
centerPoint = QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
def closeEvent(self, event):
mboxtitle = 'Message'
mboxmsg = 'Are you sure you want to quit?'
reply = QMessageBox.warning(self, mboxtitle, mboxmsg,
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = TimelapseApp()
sys.exit(app.exec_())