def event_page(self, doc): '''Add event page document information to a ".tiff" file. This method adds event_page document information to a ".tiff" file, creating it if nesecary. .. warning:: All non 2D 'image like' data is explicitly ignored. .. note:: The data in Events might be structured as an Event, an EventPage, or a "bulk event" (deprecated). The DocumentRouter base class takes care of first transforming the other representations into an EventPage and then routing them through here, so no further action is required in this class. We can assume we will always receive an EventPage. Parameters: ----------- doc : dict EventPage document ''' event_model.verify_filled(doc) descriptor = self._descriptors[doc['descriptor']] stream_name = descriptor.get('name') for field in doc['data']: for img in doc['data'][field]: # Check that the data is 2D or 3D; if not ignore it. data_key = descriptor['data_keys'][field] ndim = len(data_key['shape'] or []) if data_key['dtype'] == 'array' and 1 < ndim < 4: # there is data to be written so # create a file for this stream and field # if one does not exist yet if not self._tiff_writers.get(stream_name, {}).get(field): filename = get_prefixed_filename( file_prefix=self._file_prefix, start_doc=self._start, stream_name=stream_name, field=field) file = self._manager.open('stream_data', filename, 'xb') tw = TiffWriter(file, **self._init_kwargs) self._tiff_writers[stream_name][field] = tw # write the data img_asarray = numpy.asarray(img, dtype=self._astype) if ndim == 2: # handle 2D data just like 3D data # by adding a 3rd dimension img_asarray = numpy.expand_dims(img_asarray, axis=0) for i in range(img_asarray.shape[0]): img_asarray_2d = img_asarray[i, :] # append the image to the file tw = self._tiff_writers[stream_name][field] tw.save(img_asarray_2d, *self._kwargs)
def plot_image(data, **kwargs): p = functools.partial(plt.imshow, cmap='gray', clim=(0, 500)) # noqa p(data, **kwargs) from tifffile import TiffWriter ##File saving needs to be done properly via suite-case with open('/nsls2/xf07bm/data/pe1_data/2019/05/23/Diffraction_Image.tiff', 'xb') as f: tw = TiffWriter(f) tw.save(data)
def event(self, doc): '''Add event document information to a ".tiff" file. This method adds event document information to a ".tiff" file, creating it if necessary. .. warning:: All non 2D 'image-like' data is explicitly ignored. .. note:: The data in Events might be structured as an Event, an EventPage, or a "bulk event" (deprecated). The DocumentRouter base class takes care of first transforming the other representations into an EventPage and then routing them through here, as we require Event documents _in this case_ we overwrite both the `event` method and the `event_page` method so we can assume we will always receive an Event. Parameters: ----------- doc : dict Event document ''' event_model.verify_filled(event_model.pack_event_page(*[doc])) descriptor = self._descriptors[doc['descriptor']] stream_name = descriptor.get('name') for field in doc['data']: img = doc['data'][field] # Check that the data is 2D or 3D; if not ignore it. data_key = descriptor['data_keys'][field] ndim = len(data_key['shape'] or []) if data_key['dtype'] == 'array' and 1 < ndim < 4: img_asarray = numpy.asarray(img, dtype=self._astype) if ndim == 2: # handle 2D data just like 3D data # by adding a 3rd dimension img_asarray = numpy.expand_dims(img_asarray, axis=0) for i in range(img_asarray.shape[0]): img_asarray_2d = img_asarray[i, :] num = next(self._counter[stream_name][field]) filename = get_prefixed_filename( file_prefix=self._file_prefix, start_doc=self._start, descriptor_doc=descriptor, event_doc=doc, num=num, stream_name=stream_name, field=field) file = self._manager.open('stream_data', filename, 'xb') tw = TiffWriter(file, **self._init_kwargs) self._tiff_writers[stream_name][field + f'-{num}'] = tw tw.save(img_asarray_2d, *self._kwargs)
def event_page(self, doc): '''Add event page document information to a ".tiff" file. This method adds event_page document information to a ".tiff" file, creating it if nesecary. .. warning:: All non 2D 'image like' data is explicitly ignored. .. note:: The data in Events might be structured as an Event, an EventPage, or a "bulk event" (deprecated). The DocumentRouter base class takes care of first transforming the other repsentations into an EventPage and then routing them through here, so no further action is required in this class. We can assume we will always receive an EventPage. Parameters: ----------- doc : dict EventPage document ''' event_model.verify_filled(doc) streamname = self._descriptors[doc['descriptor']].get('name') self._templated_file_prefix = self._file_prefix.format( start=self._start) for field in doc['data']: for img in doc['data'][field]: # check that the data is 2D, if not ignore it img_asarray = numpy.asarray(img, dtype=numpy.dtype(self._astype)) if img_asarray.ndim == 2: # create a file for this stream and field if required if not self._tiff_writers.get(streamname, {}).get(field): filename = (f'{self._templated_file_prefix}' f'{streamname}-{field}.tiff') file = self._manager.open('stream_data', filename, 'xb') tw = TiffWriter(file, **self._init_kwargs) self._tiff_writers[streamname][field] = tw # append the image to the file tw = self._tiff_writers[streamname][field] tw.save(img_asarray, *self._kwargs)
def event(self, doc): '''Add event document information to a ".tiff" file. This method adds event document information to a ".tiff" file, creating it if necessary. .. warning:: All non 2D 'image-like' data is explicitly ignored. .. note:: The data in Events might be structured as an Event, an EventPage, or a "bulk event" (deprecated). The DocumentRouter base class takes care of first transforming the other repsentations into an EventPage and then routing them through here, as we require Event documents _in this case_ we overwrite both the `event` method and the `event_page` method so we can assume we will always receive an Event. Parameters: ----------- doc : dict Event document ''' event_model.verify_filled(event_model.pack_event_page(*[doc])) descriptor = self._descriptors[doc['descriptor']] streamname = descriptor.get('name') for field in doc['data']: img = doc['data'][field] # check that the data is 2D, if not ignore it img_asarray = numpy.asarray(img, dtype=self._astype) if img_asarray.ndim == 2: # template the file name. self._templated_file_prefix = self._file_prefix.format( start=self._start, descriptor=descriptor, event=doc) num = next(self._counter[streamname][field]) filename = (f'{self._templated_file_prefix}' f'{streamname}-{field}-{num}.tiff') file = self._manager.open('stream_data', filename, 'xb') tw = TiffWriter(file, **self._init_kwargs) self._tiff_writers[streamname][field + f'-{num}'] = tw tw.save(img_asarray, *self._kwargs)
class WriterHandler(BaseHandler): def sync_name(self, member, filedir, filename): path = ip1_datapath.joinpath(member, filedir) if not path.is_dir(): os.makedirs(path.str) self.tifpath = path.joinpath(filename).with_suffix('.tif') self.csvpath = path.joinpath(filename).with_suffix('.csv') return True # self.ready() def check(self, filename): pass def ready(self): if self.tifpath.is_file() or self.csvpath.is_file(): raise Exception( 'Filename already exists. Please provide new one...') else: return True def register(self): super(WriterHandler, self).register() self.ready_at = time.time() def enter(self): print 'enter' self.tif = TiffWriter(self.tifpath.str, bigtiff=True) self.csv = self.csvpath.open('w') def exposure_end(self, frame, _ts): if self.svc.bypass: return ts = time.time() - self.ready_at self.tif.save(frame) # self.tif.save(frame, extratags=[( # 306, 's', 0, str(ts), False # )]) self.csv.write(u'{}\n'.format(ts)) def exit(self): print 'exit' self.tif.close() self.csv.close()
def process(self, theSourceDir, theDestinationDir, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) if theSourceDir == theDestinationDir: self.log("Input and output directories cannot be the same.") return False fileList = glob.glob(theSourceDir+'/*.'+self.fileExtension) if len(fileList) == 0: self.log(" Zero files to process in source directory.") return False fileList = glob.glob(theSourceDir+'/*.'+self.fileExtension) self.log('Processing %i images' % len(fileList)) progress = 0 for file in fileList: #Load image data = cv2.cvtColor(cv2.imread(file), cv2.COLOR_BGR2RGB) data = data.astype('float') self.currentMax = 0 #Load original EXIF data inExif = pyexiv2.metadata.ImageMetadata(file) inExif.read() #Radiometric Calibration if self.radiometricCalibrator != None and self.radiometricCalibration: data = self.radiometricCalibrator.calibrate(data) self.currentMax = 1 #compute index if self.index == 'NDVI': data = self.indexNdvi(data) #Scale if self.currentMax != 0: percentOfRange = (data - self.currentMin) / (self.currentMax - self.currentMin) data = (self.scaleFrom * (1 - percentOfRange)) + (self.scaleTo * percentOfRange) #Set dtype if self.scaleTo == 1: if data.dtype.name != 'float32': data = data.astype('float32') elif self.scaleTo > 255: #unsigned int (I:16) data = data.astype('uint16') elif self.index != 'None': if self.lut == 'None': #bit image (L) data = data.astype('uint8') pass else: #Paletted pass else: #RGB (RGB) data = data.astype('uint8') #Save processed data to new file path, baseName = os.path.split(file) baseName = re.sub(r''+re.escape(self.fileExtension)+'$', 'tiff', baseName) if self.index != 'None': baseName = self.index+'-'+baseName tif = TiffWriter(theDestinationDir+'/'+baseName) tif.save(data, compress=6) tif = None #Transfer EXIF data to new file outExif = pyexiv2.metadata.ImageMetadata(theDestinationDir+'/'+baseName) outExif.read() #inExif.copy(outExif, comment=False) #outExif.write() #Log and progress progress += 1 self.log('%s created' % (baseName), progress=progress)
laser = TopticaiBeam(port="COM1") # initialise buffer as a queue image_buffer = Queue() # configure camera, pass the buffer queue and enable. camera.set_client(image_buffer) camera.exposure_time = exposure_seconds camera.set_trigger(TriggerType.SOFTWARE, TriggerMode.ONCE) camera.enable() # configure laser laser.power = power_level laser.set_trigger(TriggerType.HIGH, TriggerMode.BULB) laser.enable() # main loop to collect images. for i in range(n_repeats): camera.trigger() time.sleep(interval_seconds) # shutdown hardware devices laser.shutdown() camera.shutdown() # write out image data to a file. writer = TiffWriter("data.tif") for i in range(n_repeats): writer.save(image_buffer.get()) writer.close()