-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiv.py
executable file
·364 lines (289 loc) · 12.3 KB
/
siv.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/bin/env python
from __future__ import print_function
import fnmatch
import os
import re
import sys
import argparse
import numpy as np
from PIL import Image
try:
from PySide import QtGui
from PySide.QtCore import Qt, QSize
except:
from PyQt4 import QtGui
from PyQt4.QtCore import Qt, QSize
from patternreplacer import PatternReplacer
class SubsetImageModel():
def __init__(self, paths, split_tokens='-', suffix=''):
self._paths = paths
self._split_token = split_tokens
self._suffix = suffix
self._view = None
self._active_subset = None
self._glob_pattern = None
self.filenames = None
# get subdirs of imagedir
subdirs = self._paths
# use first path as pattern
print("Creating pattern")
self.pr = PatternReplacer(subdirs[0], split_tokens)
self._n_subsets = self.pr.get_n_tokens()
# all subdirs should have the same number of tokens
print("Verifying filenames")
assert all(self.pr.verify(s) for s in subdirs), "All paths must have the same number of tokens"
# add asterisks
self.subsets = [set("*") for i in range(self._n_subsets)]
for subdir in subdirs:
tokens = re.split("|".join(re.escape(t) for t in split_tokens), subdir)
tokens = [t for t in tokens if t] # remove empty ones
assert len(tokens) == self._n_subsets
for i, t in enumerate(tokens):
self.subsets[i].add(t)
self.default_subsets = ["*"] * self._n_subsets
# Mapping from changeable token index to index in complete range
self._perm = []
for i, subset in enumerate(self.subsets):
if len(subset) != 2: # * and one token
self._perm.append(i)
else:
# fix tokens which are unique. No need to glob them
assert '*' in subset
self.default_subsets[i] = (subset - set('*')).pop()
self.set_active_subset(['*'] * len(self._perm))
def _changeable_to_total(self, changeable):
# fill total pattern with changeable using perm
pass
def __total_to_changeable(self, total):
# use only the ones mentioned in perm
pass
def get_subsets(self):
# return only changeable subsets
return [sorted(self.subsets[i]) for i in self._perm]
def get_filenames(self):
return self.filenames
def set_active_subset(self, active_subset):
self._active_subset = active_subset
self._update_filenames()
def _update_filenames(self):
# glob everything
tokens = list(self.default_subsets)
# but fill the given values
for i, token in enumerate(self._active_subset):
tokens[self._perm[i]] = token
self._glob_pattern = self.pr.replace(tokens)
self.filenames = sorted(fnmatch.filter(self._paths, self._glob_pattern))
self.filenames = [fn + self._suffix for fn in self.filenames]
def get_active_subset(self):
return self._active_subset
def get_glob_pattern(self):
return self._glob_pattern
class SubsetImageController():
def __init__(self):
self._model = None
self._view = None
def set_view(self, view):
"""
@type view: SubsetImageView
"""
self._view = view
def set_model(self, model):
"""
@type model: SubsetImageModel
"""
self._model = model
def combos_changed(self):
self._model.set_active_subset(self._view.get_selected_subset())
self._view.set_filenames(self._model.get_filenames())
self._view.set_statusbar(self.get_glob_pattern())
self.filename_changed()
def get_subsets(self):
return self._model.get_subsets()
def get_filenames(self):
return self._model.get_filenames()
def filename_changed(self):
filename = self._view.get_selected_filename()
self._view.set_image(filename)
def get_glob_pattern(self):
return self._model.get_glob_pattern()
class ListWidget(QtGui.QListWidget):
def sizeHint(self):
s = QSize()
s.setHeight(super(ListWidget, self).sizeHint().height())
# hint to max text length
# width from scrollbar is too big? just use half of it.
s.setWidth(self.sizeHintForColumn(0) + self.verticalScrollBar().width() / 2)
return s
class SubsetImageView(QtGui.QWidget):
def __init__(self, controller, parent=None):
"""
@param controller:
@type controller: SubsetImageController
"""
QtGui.QWidget.__init__(self, parent)
self._controller = controller
controller.set_view(self)
self.setWindowTitle('Subset image viewer')
# Upper combo boxes
hbox_upper = QtGui.QHBoxLayout()
# Statically set combos
subsets = controller.get_subsets()
if len(subsets) == 1:
subsets = []
self.combos = []
for subset in subsets:
combo = QtGui.QComboBox()
combo.addItems(subset)
combo.currentIndexChanged.connect(self._combos_changed)
self.combos.append(combo)
hbox_upper.addWidget(combo, 1)
self.anti_alias = QtGui.QCheckBox()
self.anti_alias.setText("AA")
self.anti_alias.setToolTip("anti-aliasing (F1)")
self.anti_alias.setFixedSize(self.anti_alias.sizeHint())
self.anti_alias.stateChanged.connect(self.resizeEvent)
hbox_upper.addWidget(self.anti_alias, 0, Qt.AlignRight)
# Image display
self.image = QtGui.QPixmap()
self.image_widget = QtGui.QLabel()
# allow widget to decreased in size
self.image_widget.setMinimumSize(1, 1)
# filename list
self.filenames = ListWidget()
self.filenames.currentItemChanged.connect(controller.filename_changed)
self.filenames.updateGeometry()
# open collapsed
if len(controller.get_filenames()) > 5000:
print("Found more than 5000 images. Preselecting subsets for faster loading.")
for combo in self.combos:
combo.setCurrentIndex(1)
# trigger loading of filename list
self._combos_changed()
hbox_lower = QtGui.QSplitter(Qt.Horizontal)
hbox_lower.splitterMoved.connect(self.resizeEvent)
hbox_lower.addWidget(self.image_widget)
hbox_lower.addWidget(self.filenames)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(hbox_upper)
vbox.addWidget(hbox_lower)
self.setLayout(vbox)
# select first file on startup
if self.filenames.count() > 0:
self.filenames.setCurrentRow(0)
controller.filename_changed()
# initial focus on list
self.filenames.setFocus()
def keyPressEvent(self, key_event):
# anti alias keyboard shortcut
if key_event.key() == Qt.Key_F1:
self.anti_alias.nextCheckState()
# copy current filename to clipboard
if key_event.key() == Qt.Key_F2:
clipboard = QtGui.QApplication.clipboard()
clipboard.setText(self.get_selected_filename())
if key_event.key() == Qt.Key_F3:
clipboard = QtGui.QApplication.clipboard()
clipboard.setText(self._controller.get_glob_pattern())
def get_selected_subset(self):
selected_combos = []
for c in self.combos:
assert isinstance(c, QtGui.QComboBox)
selected_combos.append(str(c.currentText()))
return selected_combos
def get_selected_filename(self):
current_item = self.filenames.currentItem()
if current_item is not None:
return current_item.text()
else:
return None
def set_image(self, filename):
if not filename:
return
if not os.path.exists(filename):
self.image_widget.setText("File not found: \n" + filename)
else:
is_monochrome_16_tiff = False
image_reader = QtGui.QImageReader(filename)
# use pil to decide for 16 bit tiff
i = Image.open(filename)
if(image_reader.format() == 'tiff'):
if i.mode == 'I;16':
is_monochrome_16_tiff = True
# open 16 bit monochrome tiff using PIL and make drawable 8 bit out of it
if is_monochrome_16_tiff:
# np array from pil image
# TODO maybe use pil to get rid of numpy dependency
image = np.array(i.getdata()).reshape(i.size[0], i.size[1])
# # use leftmost 8 bit starting from first 1
shift = int(np.ceil(np.log2(np.max(image)))) - 8
shift = max(shift, 0) # use rightmost 8 bit, if max is smaller than 255
image >>= shift
# assemble QImage data structure
image = (255 << 24 | image << 16 | image << 8 | image) # ARGB from grayscale
image = image.astype(np.uint32)
image = QtGui.QImage(image.ravel(), image.shape[0], image.shape[1], QtGui.QImage.Format_RGB32).copy()
else:
image = image_reader.read()
self.image = QtGui.QPixmap(image)
self.image_widget.setPixmap(self.image)
self.resizeEvent()
def set_filenames(self, filenames):
self.filenames.clear()
self.filenames.addItems(filenames)
self.filenames.setMaximumWidth(self.filenames.sizeHint().width())
def resizeEvent(self, resize_event=None):
if self.image and not self.image.isNull():
transformation_mode = Qt.SmoothTransformation if self.anti_alias.checkState() else Qt.FastTransformation
self.image_widget.setPixmap(
self.image.scaled(self.image_widget.size(), Qt.KeepAspectRatio, transformation_mode))
def set_statusbar(self, message, time=10000):
self.parent().statusBar().showMessage(message, time)
def _combos_changed(self):
current_item_text = self.filenames.currentItem().text() if self.filenames.currentItem() is not None else None
self._controller.combos_changed()
item_in_updated_list = self.filenames.findItems(current_item_text, Qt.MatchExactly)\
if current_item_text is not None else None
if item_in_updated_list:
# use previously selected item in new list
self.filenames.setCurrentItem(item_in_updated_list[0])
else:
# otherwise select first item
self.filenames.setCurrentRow(0)
def main():
parser = argparse.ArgumentParser(description='Subset image viewer.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--suffix', '-s', default='', help='suffix which is appended to all paths')
parser.add_argument('--delimiters', '-d', default='@', help='delimiters for splitting paths')
parser.add_argument('--ignore-missing', '-i', action='store_true', help='ignore missing paths')
parser.add_argument('paths', nargs='+', help='image file names. Use - to read file names from stdin')
args = parser.parse_args()
if args.paths == ['-']:
print("Reading file names from stdin")
paths = [l.strip() for l in sys.stdin.readlines()]
else:
paths = args.paths
if args.ignore_missing:
valid_paths = []
for path in paths:
filename = path + args.suffix
if not os.path.exists(filename):
sys.stderr.write("Ignoring missing file: %s\n" % filename)
elif not os.path.isfile(filename):
sys.stderr.write("Ignoring directory: %s\n" % filename)
else:
valid_paths.append(path)
paths = valid_paths
print("%d files loaded" % len(paths))
app = QtGui.QApplication(sys.argv)
sim = SubsetImageModel(paths=paths, split_tokens=args.delimiters, suffix=args.suffix)
sic = SubsetImageController()
sic.set_model(sim)
main_window = QtGui.QMainWindow()
siv = SubsetImageView(sic, main_window)
main_window.setWindowTitle("siv - Subset Image Viewer")
main_window.setCentralWidget(siv)
print("Loading GUI")
main_window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()