def test_rgb_tiles(self): def getSubData(dast, zoom, rect): x1, y1, x2, y2 = rect tiles = [] for x in range(x1, x2 + 1): tiles_column = [] for y in range(y1, y2 + 1): tiles_column.append(dast.getTile(x, y, zoom)) tiles.append(tiles_column) return tiles FILENAME = u"test" + tiff.EXTENSIONS[0] POS = (5.0, 7.0) size = (3, 2000, 1000) dtype = numpy.uint8 md = { model.MD_DIMS: 'YXC', model.MD_POS: POS, model.MD_PIXEL_SIZE: (1e-6, 1e-6), } arr = numpy.array(range(size[0] * size[1] * size[2])).reshape( size[::-1]).astype(dtype) print(arr.shape) data = model.DataArray(arr, metadata=md) # export tiff.export(FILENAME, data, pyramid=True) rdata = tiff.open_data(FILENAME) tiles = getSubData(rdata.content[0], 0, (0, 0, 7, 3)) merged_img = img.mergeTiles(tiles) self.assertEqual(merged_img.shape, (1000, 2000, 3)) self.assertEqual(merged_img.metadata[model.MD_POS], POS) tiles = getSubData(rdata.content[0], 0, (0, 0, 3, 1)) merged_img = img.mergeTiles(tiles) self.assertEqual(merged_img.shape, (512, 1024, 3)) numpy.testing.assert_almost_equal(merged_img.metadata[model.MD_POS], (4.999512, 7.000244)) del rdata os.remove(FILENAME)
def test_rgb_tiles(self): def getSubData(dast, zoom, rect): x1, y1, x2, y2 = rect tiles = [] for x in range(x1, x2 + 1): tiles_column = [] for y in range(y1, y2 + 1): tiles_column.append(dast.getTile(x, y, zoom)) tiles.append(tiles_column) return tiles FILENAME = u"test" + tiff.EXTENSIONS[0] POS = (5.0, 7.0) size = (3, 2000, 1000) dtype = numpy.uint8 md = { model.MD_DIMS: 'YXC', model.MD_POS: POS, model.MD_PIXEL_SIZE: (1e-6, 1e-6), } arr = numpy.array(range(size[0] * size[1] * size[2])).reshape(size[::-1]).astype(dtype) print(arr.shape) data = model.DataArray(arr, metadata=md) # export tiff.export(FILENAME, data, pyramid=True) rdata = tiff.open_data(FILENAME) tiles = getSubData(rdata.content[0], 0, (0, 0, 7, 3)) merged_img = img.mergeTiles(tiles) self.assertEqual(merged_img.shape, (1000, 2000, 3)) self.assertEqual(merged_img.metadata[model.MD_POS], POS) tiles = getSubData(rdata.content[0], 0, (0, 0, 3, 1)) merged_img = img.mergeTiles(tiles) self.assertEqual(merged_img.shape, (512, 1024, 3)) numpy.testing.assert_almost_equal(merged_img.metadata[model.MD_POS], (4.999512, 7.000244)) del rdata os.remove(FILENAME)
def projectAsRaw(self): """ Project a raw image without converting to RGB Handles tiles as well as regular DataArray's """ raw = self.stream.raw if isinstance(raw[0], model.DataArrayShadow): raw_tiles, _ = self._getTilesFromSelectedArea() raw = img.mergeTiles(raw_tiles) return raw else: return super(RGBSpatialProjection, self).projectAsRaw()
def _getMergedRawImage(self, z): """ Returns the merged image based on z and .rect, using the raw tiles (not projected) z (int): Zoom level index return (DataArray): The merged image """ # calculates the size of the merged image width_zoomed = self._das.shape[1] / (2 ** z) height_zoomed = self._das.shape[0] / (2 ** z) # calculates the number of tiles on both axes num_tiles_x = int(math.ceil(width_zoomed / self._das.tile_shape[1])) num_tiles_y = int(math.ceil(height_zoomed/ self._das.tile_shape[0])) tiles = [] for x in range(num_tiles_x): tiles_column = [] for y in range(num_tiles_y): tile = self._das.getTile(x, y, z) tiles_column.append(tile) tiles.append(tiles_column) return img.mergeTiles(tiles)
def _getMergedRawImage(self, das, z): """ Returns the entire raw data of DataArrayShadow at a given zoom level das (DataArrayShadow): shadow of the raw data z (int): Zoom level index return (DataArray): The merged image """ # calculates the size of the merged image width_zoomed = das.shape[1] / (2 ** z) height_zoomed = das.shape[0] / (2 ** z) # calculates the number of tiles on both axes num_tiles_x = int(math.ceil(width_zoomed / das.tile_shape[1])) num_tiles_y = int(math.ceil(height_zoomed / das.tile_shape[0])) tiles = [] for x in range(num_tiles_x): tiles_column = [] for y in range(num_tiles_y): tile = das.getTile(x, y, z) tiles_column.append(tile) tiles.append(tiles_column) return img.mergeTiles(tiles)
def test_one_tile(self): def getSubData(dast, zoom, rect): x1, y1, x2, y2 = rect tiles = [] for x in range(x1, x2 + 1): tiles_column = [] for y in range(y1, y2 + 1): tiles_column.append(dast.getTile(x, y, zoom)) tiles.append(tiles_column) return tiles FILENAME = u"test" + tiff.EXTENSIONS[0] POS = (5.0, 7.0) size = (250, 200) md = { model.MD_DIMS: 'YX', model.MD_POS: POS, model.MD_PIXEL_SIZE: (1e-6, 1e-6), } arr = numpy.arange(size[0] * size[1], dtype=numpy.uint8).reshape(size[::-1]) data = model.DataArray(arr, metadata=md) # export tiff.export(FILENAME, data, pyramid=True) rdata = tiff.open_data(FILENAME) tiles = getSubData(rdata.content[0], 0, (0, 0, 0, 0)) merged_img = img.mergeTiles(tiles) self.assertEqual(merged_img.shape, (200, 250)) self.assertEqual(merged_img.metadata[model.MD_POS], POS) del rdata os.remove(FILENAME)
def snapshot_viewport(self, tab, filepath, exporter, anim): """ Save a snapshot of the raw image from the focused view to the filesystem. :param tab: (Tab) the current tab to save the snapshot from :param filepath: (str) full path to the destination file :param exporter: (func) exporter to use for writing the file :param anim: (bool) if True will show an animation When no dialog is shown, the name of the file will follow the scheme `date`-`time`.tiff (e.g., 20120808-154812.tiff) and it will be saved in the user's picture directory. """ try: tab_data_model = tab.tab_data_model # Take all the streams available streams = tab_data_model.streams.value if not streams: logging.info("Failed to take snapshot, no stream in tab %s", tab.name) return if anim: self.start_snapshot_animation() # get currently focused view view = tab_data_model.focussedView.value if not view: try: view = tab_data_model.views.value[0] except IndexError: view = None # let's try to get a thumbnail if not view or view.thumbnail.value is None: thumbnail = None else: # need to convert from wx.Image to ndimage thumbnail = img.wxImage2NDImage(view.thumbnail.value, keep_alpha=False) # add some basic info to the image mpp = view.mpp.value metadata = { model.MD_POS: view.view_pos.value, model.MD_PIXEL_SIZE: (mpp, mpp), model.MD_DESCRIPTION: "Composited image preview" } thumbnail = model.DataArray(thumbnail, metadata=metadata) # for each stream seen in the viewport raw_images = [] for s in streams: data = s.raw if isinstance(data, tuple): # 2D tuple = tiles data = [mergeTiles(data)] for d in data: # add the stream name to the image if not hasattr(d, "metadata"): # Not a DataArray => let's try to convert it try: d = model.DataArray(d) except Exception: logging.warning( "Raw data of stream %s doesn't seem to be DataArray", s.name.value) continue if model.MD_DESCRIPTION not in d.metadata: d.metadata[model.MD_DESCRIPTION] = s.name.value raw_images.append(d) popup.show_message(self._main_frame, "Snapshot saved in %s" % (filepath, ), timeout=3) # record everything to a file exporter.export(filepath, raw_images, thumbnail) logging.info("Snapshot saved as file '%s'.", filepath) except Exception: logging.exception("Failed to save snapshot")
def snapshot_viewport(self, tab, filepath, exporter, anim): """ Save a snapshot of the raw image from the focused view to the filesystem. :param tab: (Tab) the current tab to save the snapshot from :param filepath: (str) full path to the destination file :param exporter: (func) exporter to use for writing the file :param anim: (bool) if True will show an animation When no dialog is shown, the name of the file will follow the scheme `date`-`time`.tiff (e.g., 20120808-154812.tiff) and it will be saved in the user's picture directory. """ try: tab_data_model = tab.tab_data_model # Take all the streams available streams = tab_data_model.streams.value if not streams: logging.info("Failed to take snapshot, no stream in tab %s", tab.name) return if anim: self.start_snapshot_animation() # get currently focused view view = tab_data_model.focussedView.value if not view: try: view = tab_data_model.views.value[0] except IndexError: view = None # let's try to get a thumbnail if not view or view.thumbnail.value is None: thumbnail = None else: # need to convert from wx.Image to ndimage thumbnail = img.wxImage2NDImage(view.thumbnail.value, keep_alpha=False) # add some basic info to the image mpp = view.mpp.value metadata = {model.MD_POS: view.view_pos.value, model.MD_PIXEL_SIZE: (mpp, mpp), model.MD_DESCRIPTION: "Composited image preview"} thumbnail = model.DataArray(thumbnail, metadata=metadata) # for each stream seen in the viewport raw_images = [] for s in streams: data = s.raw if isinstance(data, tuple): # 2D tuple = tiles data = [mergeTiles(data)] for d in data: # add the stream name to the image if not hasattr(d, "metadata"): # Not a DataArray => let's try to convert it try: d = model.DataArray(d) except Exception: logging.warning("Raw data of stream %s doesn't seem to be DataArray", s.name.value) continue if model.MD_DESCRIPTION not in d.metadata: d.metadata[model.MD_DESCRIPTION] = s.name.value raw_images.append(d) popup.show_message(self._main_frame, "Snapshot saved in %s" % (filepath,), timeout=3 ) # record everything to a file exporter.export(filepath, raw_images, thumbnail) logging.info("Snapshot saved as file '%s'.", filepath) except Exception: logging.exception("Failed to save snapshot")