def execute_command(command, options = None): '''Execute the named command within ImageJ''' def fn(command=command, options=options): if options is None: J.static_call("ij/IJ", "run", "(Ljava/lang/String;)V", command) else: J.static_call("ij/IJ", "run", "(Ljava/lang/String;Ljava/lang/String;)V", command, options) J.run_in_main_thread(fn, True)
def execute_macro(macro_text): '''Execute a macro in ImageJ macro_text - the macro program to be run ''' def fn(macro_text = macro_text): show_imagej() interp = J.make_instance("ij/macro/Interpreter","()V") J.call(interp, "run","(Ljava/lang/String;)V", macro_text) J.run_in_main_thread(fn, True)
def execute_macro(macro_text): '''Execute a macro in ImageJ macro_text - the macro program to be run ''' def fn(macro_text=macro_text): show_imagej() interp = J.make_instance("ij/macro/Interpreter", "()V") J.call(interp, "run", "(Ljava/lang/String;)V", macro_text) J.run_in_main_thread(fn, True)
def execute_command(command, options=None): '''Execute the named command within ImageJ''' def fn(command=command, options=options): if options is None: J.static_call("ij/IJ", "run", "(Ljava/lang/String;)V", command) else: J.static_call("ij/IJ", "run", "(Ljava/lang/String;Ljava/lang/String;)V", command, options) J.run_in_main_thread(fn, True)
def on_show_imagej(self): '''Show the ImageJ user interface This method shows the ImageJ user interface when the user presses the Show ImageJ button. ''' if bioformats.USE_IJ2: J.run_in_main_thread( lambda: RunImageJ.get_context().loadService( "imagej.ui.UIService"), True) else: import imagej.ijbridge as ijbridge ijb = ijbridge.get_ij_bridge() J.run_in_main_thread(lambda: ijb.show_imagej(), True)
def on_show_imagej(self): '''Show the ImageJ user interface This method shows the ImageJ user interface when the user presses the Show ImageJ button. ''' if bioformats.USE_IJ2: J.run_in_main_thread( lambda : RunImageJ.get_context().loadService("imagej.ui.UIService"), True) else: import imagej.ijbridge as ijbridge ijb = ijbridge.get_ij_bridge() ijb.show_imagej()
def get_commands(): '''Return a list of the available command strings''' def fn(): hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: # # This is a little bogus, but works - trick IJ into initializing # execute_command("pleaseignorethis") hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: return [] keys = J.call(hashtable, "keys", "()Ljava/util/Enumeration;") keys = J.jenumeration_to_string_list(keys) values = J.call(hashtable, "values", "()Ljava/util/Collection;") values = [J.to_string(x) for x in J.iterate_java( J.call(values, 'iterator', "()Ljava/util/Iterator;"))] class CommandList(list): def __init__(self): super(CommandList, self).__init__(keys) self.values = values return CommandList() return J.run_in_main_thread(fn, True)
def get_commands(): '''Return a list of the available command strings''' def fn(): hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: # # This is a little bogus, but works - trick IJ into initializing # execute_command("pleaseignorethis") hashtable = J.static_call('ij/Menus', 'getCommands', '()Ljava/util/Hashtable;') if hashtable is None: return [] keys = J.call(hashtable, "keys", "()Ljava/util/Enumeration;") keys = J.jenumeration_to_string_list(keys) values = J.call(hashtable, "values", "()Ljava/util/Collection;") values = [ J.to_string(x) for x in J.iterate_java( J.call(values, 'iterator', "()Ljava/util/Iterator;")) ] class CommandList(list): def __init__(self): super(CommandList, self).__init__(keys) self.values = values return CommandList() return J.run_in_main_thread(fn, True)
def __init__(self): classes = [J.class_for_name(x) for x in service_classes] self.o = J.run_in_main_thread( lambda: J.static_call("imagej/ImageJ", "createContext", "([Ljava/lang/Class;)Limagej/ImageJ;", classes), True)
def __init__(self): classes = [ J.class_for_name(x) for x in service_classes] self.o = J.run_in_main_thread( lambda :J.static_call( "imagej/ImageJ", "createContext", "([Ljava/lang/Class;)Limagej/ImageJ;", classes), True)
def load_using_bioformats(path, c=None, z=0, t=0, series=None, index=None, rescale = True, wants_max_intensity = False, channel_names = None): '''Load the given image file using the Bioformats library path: path to the file z: the frame index in the z (depth) dimension. t: the frame index in the time dimension. channel_names: None if you don't want them, a list which will be filled if you do Returns either a 2-d (grayscale) or 3-d (2-d + 3 RGB planes) image ''' def fn(path=path, c=c, z=z, t=t, series=series, index=index, rescale=rescale, wants_max_intensity=wants_max_intensity, channel_names=channel_names): FormatTools = make_format_tools_class() ImageReader = make_image_reader_class() ChannelSeparator = make_reader_wrapper_class( "loci/formats/ChannelSeparator") # # Bioformats is more picky about slashes than Python # if sys.platform.startswith("win"): path = path.replace("/",os.path.sep) # # Bypass the ImageReader and scroll through the class list. The # goal here is to ask the FormatHandler if it thinks it could # possibly parse the file, then only give the FormatReader access # to the open file stream so it can't damage the file server. # env = jutil.get_env() class_list = get_class_list() stream = jutil.make_instance('loci/common/RandomAccessInputStream', '(Ljava/lang/String;)V', path) filename = os.path.split(path)[1] IFormatReader = make_iformat_reader_class() rdr = None for klass in env.get_object_array_elements(class_list.get_classes()): wclass = jutil.get_class_wrapper(klass, True) maybe_rdr = IFormatReader() maybe_rdr.o = wclass.newInstance() maybe_rdr.setGroupFiles(False) if maybe_rdr.suffixNecessary: if not maybe_rdr.isThisTypeSZ(filename, False): continue if maybe_rdr.suffixSufficient: rdr = maybe_rdr break if (maybe_rdr.isThisTypeStream(stream)): rdr = maybe_rdr break if rdr is None: raise ValueError("Could not find a Bio-Formats reader for %s", path) mdoptions = metadatatools.get_metadata_options(metadatatools.ALL) rdr.setMetadataOptions(mdoptions) metadata = metadatatools.createOMEXMLMetadata() rdr.setMetadataStore(metadata) rdr.setId(path) width = rdr.getSizeX() height = rdr.getSizeY() pixel_type = rdr.getPixelType() little_endian = rdr.isLittleEndian() if pixel_type == FormatTools.INT8: dtype = np.char scale = 255 elif pixel_type == FormatTools.UINT8: dtype = np.uint8 scale = 255 elif pixel_type == FormatTools.UINT16: dtype = '<u2' if little_endian else '>u2' scale = 65535 elif pixel_type == FormatTools.INT16: dtype = '<i2' if little_endian else '>i2' scale = 65535 elif pixel_type == FormatTools.UINT32: dtype = '<u4' if little_endian else '>u4' scale = 2**32 elif pixel_type == FormatTools.INT32: dtype = '<i4' if little_endian else '>i4' scale = 2**32-1 elif pixel_type == FormatTools.FLOAT: dtype = '<f4' if little_endian else '>f4' scale = 1 elif pixel_type == FormatTools.DOUBLE: dtype = '<f8' if little_endian else '>f8' scale = 1 max_sample_value = rdr.getMetadataValue('MaxSampleValue') if max_sample_value is not None: try: scale = jutil.call(max_sample_value, 'intValue', '()I') except: bioformats.logger.warning("WARNING: failed to get MaxSampleValue for image. Intensities may be improperly scaled.") if series is not None: rdr.setSeries(series) if index is not None: image = np.frombuffer(rdr.openBytes(index), dtype) if len(image) / height / width in (3,4): image.shape = (height, width, int(len(image) / height / width)) else: image.shape = (height, width) elif rdr.isRGB() and rdr.isInterleaved(): index = rdr.getIndex(z,0,t) image = np.frombuffer(rdr.openBytes(index), dtype) image.shape = (height, width, 3) elif c is not None and rdr.getRGBChannelCount() == 1: index = rdr.getIndex(z,c,t) image = np.frombuffer(rdr.openBytes(index), dtype) image.shape = (height, width) elif rdr.getRGBChannelCount() > 1: rdr.close() rdr = ImageReader() rdr.allowOpenToCheckType(False) rdr = ChannelSeparator(rdr) rdr.setGroupFiles(False) rdr.setId(path) red_image, green_image, blue_image = [ np.frombuffer(rdr.openBytes(rdr.getIndex(z,i,t)),dtype) for i in range(3)] image = np.dstack((red_image, green_image, blue_image)) image.shape=(height,width,3) elif rdr.getSizeC() > 1: images = [np.frombuffer(rdr.openBytes(rdr.getIndex(z,i,t)), dtype) for i in range(rdr.getSizeC())] image = np.dstack(images) image.shape = (height, width, rdr.getSizeC()) if not channel_names is None: metadata = metadatatools.MetadataRetrieve(metadata) for i in range(rdr.getSizeC()): index = rdr.getIndex(z, 0, t) channel_name = metadata.getChannelName(index, i) if channel_name is None: channel_name = metadata.getChannelID(index, i) channel_names.append(channel_name) else: index = rdr.getIndex(z,0,t) image = np.frombuffer(rdr.openBytes(index),dtype) image.shape = (height,width) rdr.close() jutil.call(stream, 'close', '()V') del rdr # # Run the Java garbage collector here. # jutil.static_call("java/lang/System", "gc","()V") if rescale: image = image.astype(np.float32) / float(scale) if wants_max_intensity: return image, scale return image return jutil.run_in_main_thread(fn, True)