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
|
# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016-2021 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# ###########################################################################*/
"""
This module contains an :class:`ImageFileDialog`.
"""
__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "05/03/2019"
import logging
from silx.gui.plot import actions
from silx.gui import qt
from silx.gui.plot.PlotWidget import PlotWidget
from .AbstractDataFileDialog import AbstractDataFileDialog
import silx.io
_logger = logging.getLogger(__name__)
class _ImageSelection(qt.QWidget):
"""Provide a widget allowing to select an image from an hypercube by
selecting a slice."""
selectionChanged = qt.Signal()
"""Emitted when the selection change."""
def __init__(self, parent=None):
qt.QWidget.__init__(self, parent)
self.__shape = None
self.__axis = []
layout = qt.QVBoxLayout()
self.setLayout(layout)
def hasVisibleSelectors(self):
return self.__visibleSliders > 0
def isUsed(self):
if self.__shape is None:
return False
return len(self.__shape) > 2
def getSelectedData(self, data):
slicing = self.slicing()
image = data[slicing]
return image
def setData(self, data):
if data is None:
self.__visibleSliders = 0
return
shape = data.shape
if self.__shape is not None:
# clean up
for widget in self.__axis:
self.layout().removeWidget(widget)
widget.deleteLater()
self.__axis = []
self.__shape = shape
self.__visibleSliders = 0
if shape is not None:
# create expected axes
for index in range(len(shape) - 2):
axis = qt.QSlider(self)
axis.setMinimum(0)
axis.setMaximum(shape[index] - 1)
axis.setOrientation(qt.Qt.Horizontal)
if shape[index] == 1:
axis.setVisible(False)
else:
self.__visibleSliders += 1
axis.valueChanged.connect(self.__axisValueChanged)
self.layout().addWidget(axis)
self.__axis.append(axis)
self.selectionChanged.emit()
def __axisValueChanged(self):
self.selectionChanged.emit()
def slicing(self):
slicing = []
for axes in self.__axis:
slicing.append(axes.value())
return tuple(slicing)
def setSlicing(self, slicing):
for i, value in enumerate(slicing):
if i > len(self.__axis):
break
self.__axis[i].setValue(value)
def selectSlicing(self, slicing):
"""Select a slicing.
The provided value could be unconsistent and therefore is not supposed
to be retrivable with a getter.
:param Union[None,Tuple[int]] slicing:
"""
if slicing is None:
# Create a default slicing
needed = self.__visibleSliders
slicing = (0,) * needed
if len(slicing) < self.__visibleSliders:
slicing = slicing + (0,) * (self.__visibleSliders - len(slicing))
self.setSlicing(slicing)
class _ImagePreview(qt.QWidget):
"""Provide a preview of the selected image"""
def __init__(self, parent=None):
super(_ImagePreview, self).__init__(parent)
self.__data = None
self.__plot = PlotWidget(self)
self.__plot.setAxesDisplayed(False)
self.__plot.setKeepDataAspectRatio(True)
layout = qt.QVBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.__plot)
self.setLayout(layout)
def resizeEvent(self, event):
self.__updateConstraints()
return qt.QWidget.resizeEvent(self, event)
def sizeHint(self):
return qt.QSize(200, 200)
def plot(self):
return self.__plot
def setData(self, data, fromDataSelector=False):
if data is None:
self.clear()
return
resetzoom = not fromDataSelector
previousImage = self.data()
if previousImage is not None and data.shape != previousImage.shape:
resetzoom = True
self.__plot.addImage(legend="data", data=data, resetzoom=resetzoom)
self.__data = data
self.__updateConstraints()
def __updateConstraints(self):
"""
Update the constraints depending on the size of the widget
"""
image = self.data()
if image is None:
return
size = self.size()
if size.width() == 0 or size.height() == 0:
return
heightData, widthData = image.shape
widthContraint = heightData * size.width() / size.height()
if widthContraint > widthData:
heightContraint = heightData
else:
heightContraint = heightData * size.height() / size.width()
widthContraint = widthData
midWidth, midHeight = widthData * 0.5, heightData * 0.5
heightContraint, widthContraint = heightContraint * 0.5, widthContraint * 0.5
axis = self.__plot.getXAxis()
axis.setLimitsConstraints(midWidth - widthContraint, midWidth + widthContraint)
axis = self.__plot.getYAxis()
axis.setLimitsConstraints(midHeight - heightContraint, midHeight + heightContraint)
def __imageItem(self):
image = self.__plot.getImage("data")
return image
def data(self):
if self.__data is not None:
if hasattr(self.__data, "name"):
# in case of HDF5
if self.__data.name is None:
# The dataset was closed
self.__data = None
return self.__data
def colormap(self):
image = self.__imageItem()
if image is not None:
return image.getColormap()
return self.__plot.getDefaultColormap()
def setColormap(self, colormap):
self.__plot.setDefaultColormap(colormap)
def clear(self):
self.__data = None
image = self.__imageItem()
if image is not None:
self.__plot.removeImage(legend="data")
class ImageFileDialog(AbstractDataFileDialog):
"""The `ImageFileDialog` class provides a dialog that allow users to select
an image from a file.
The `ImageFileDialog` class enables a user to traverse the file system in
order to select one file. Then to traverse the file to select a frame or
a slice of a dataset.
.. image:: img/imagefiledialog_h5.png
It supports fast access to image files using `FabIO`. Which is not the case
of the default silx API. Image files still also can be available using the
NeXus layout, by editing the file type combo box.
.. image:: img/imagefiledialog_edf.png
The selected data is an numpy array with 2 dimension.
Using an `ImageFileDialog` can be done like that.
.. code-block:: python
dialog = ImageFileDialog()
result = dialog.exec()
if result:
print("Selection:")
print(dialog.selectedFile())
print(dialog.selectedUrl())
print(dialog.selectedImage())
else:
print("Nothing selected")
"""
def selectedImage(self):
"""Returns the selected image data as numpy
:rtype: numpy.ndarray
"""
url = self.selectedUrl()
return silx.io.get_data(url)
def _createPreviewWidget(self, parent):
previewWidget = _ImagePreview(parent)
previewWidget.setSizePolicy(qt.QSizePolicy.Expanding, qt.QSizePolicy.Expanding)
return previewWidget
def _createSelectorWidget(self, parent):
return _ImageSelection(parent)
def _createPreviewToolbar(self, parent, dataPreviewWidget, dataSelectorWidget):
plot = dataPreviewWidget.plot()
toolbar = qt.QToolBar(parent)
toolbar.setIconSize(qt.QSize(16, 16))
toolbar.setStyleSheet("QToolBar { border: 0px }")
toolbar.addAction(actions.mode.ZoomModeAction(plot, parent))
toolbar.addAction(actions.mode.PanModeAction(plot, parent))
toolbar.addSeparator()
toolbar.addAction(actions.control.ResetZoomAction(plot, parent))
toolbar.addSeparator()
toolbar.addAction(actions.control.ColormapAction(plot, parent))
return toolbar
def _isDataSupportable(self, data):
"""Check if the selected data can be supported at one point.
If true, the data selector will be checked and it will update the data
preview. Else the selecting is disabled.
:rtype: bool
"""
if not hasattr(data, "dtype"):
# It is not an HDF5 dataset nor a fabio image wrapper
return False
if data is None or data.shape is None:
return False
if data.dtype.kind not in set(["f", "u", "i", "b"]):
return False
dim = len(data.shape)
return dim >= 2
def _isFabioFilesSupported(self):
return True
def _isDataSupported(self, data):
"""Check if the data can be returned by the dialog.
If true, this data can be returned by the dialog and the open button
while be enabled. If false the button will be disabled.
:rtype: bool
"""
dim = len(data.shape)
return dim == 2
def _displayedDataInfo(self, dataBeforeSelection, dataAfterSelection):
"""Returns the text displayed under the data preview.
This zone is used to display error in case or problem of data selection
or problems with IO.
:param numpy.ndarray dataAfterSelection: Data as it is after the
selection widget (basically the data from the preview widget)
:param numpy.ndarray dataAfterSelection: Data as it is before the
selection widget (basically the data from the browsing widget)
:rtype: bool
"""
destination = self.__formatShape(dataAfterSelection.shape)
source = self.__formatShape(dataBeforeSelection.shape)
return u"%s \u2192 %s" % (source, destination)
def __formatShape(self, shape):
result = []
for s in shape:
if isinstance(s, slice):
v = u"\u2026"
else:
v = str(s)
result.append(v)
return u" \u00D7 ".join(result)
|