def import_image(filename, color_mode='color', split_c=False, split_z=False, split_t=False): """Open an image file using the Bio-Formats importer. Parameters ---------- filename : str The full path to the file to be imported through Bio-Formats. color_mode : str, optional The color mode to be used for the resulting ImagePlus, one of 'color', 'composite', 'gray' and 'default'. split_c : bool, optional Whether to split the channels into separate ImagePlus objects. split_z : bool, optional Whether to split the z-slices into separate ImagePlus objects. split_t : bool, optional Whether to split the time points into separate ImagePlus objects. Returns ------- ij.ImagePlus[] A list of ImagePlus objects resulting from the import. """ options = ImporterOptions() mode = { 'color' : ImporterOptions.COLOR_MODE_COLORIZED, 'composite' : ImporterOptions.COLOR_MODE_COMPOSITE, 'gray' : ImporterOptions.COLOR_MODE_GRAYSCALE, 'default' : ImporterOptions.COLOR_MODE_DEFAULT, } options.setColorMode(mode[color_mode]) options.setSplitChannels(split_c) options.setSplitFocalPlanes(split_z) options.setSplitTimepoints(split_t) options.setId(filename) log.info("Reading [%s]", filename) orig_imps = BF.openImagePlus(options) log.debug("Opened [%s] %s", filename, type(orig_imps)) return orig_imps
#Define results output folder folder = input_folder+"timepoints/" if not os.path.exists(folder): os.mkdir(folder) # collect list of files to be processed #file_list = [filename for filename in os.listdir(input_folder) if ".tif" in filename] # Recursive option file_list = [os.path.join(dp, f) for dp, dn, fn in os.walk(input_folder) for f in fn if '.tif' in f] for file_path in file_list: # read in and display ImagePlus(es) with arguments options = ImporterOptions() options.setId(file_path) options.setSplitTimepoints(True) imps = BF.openImagePlus(options) for imp in imps: imp.show() name = imp.getTitle() details = name.split("_")[0] print file_path pos = details.split('Pos')[1] time = name.split("=")[1] image_type = re.split(r'.lif |- |_|\\', file_path)[-3] mutant = re.split(r'.lif |- |_|\\', file_path)[-6] cotrans = re.split(r'.lif |- |_|\\', file_path)[-5] new_name = mutant+"_"+cotrans+"_pos_"+pos+"_t_"+time+"_"+image_type print new_name IJ.saveAs(imp, "Tiff", os.path.join(folder, new_name))
def imageprojector(channels, timelist_unsorted, dirs): """ Projects .lif timepoints and saves in a common directory, as well as channel separated directories. """ # Defines in path path = str(Experiment) # BF Importer options = ImporterOptions() try: options.setId(path) except Exception(e): print str(e) options.setOpenAllSeries(True) options.setSplitTimepoints(True) options.setSplitChannels(True) imps = BF.openImagePlus(options) timelist = [x for item in timelist_unsorted for x in repeat(item, channels)] timelist, imps = zip(*sorted(zip(timelist, imps))) counter_C0 = -1 counter_C1 = -1 counter_C2 = -1 # Opens all images, splits channels, z-projects and saves to disk for imp in (imps): # Projection, Sum Intensity project = ZProjector() project.setMethod(ZProjector.SUM_METHOD) project.setImage(imp) project.doProjection() impout = project.getProjection() projection = impout.getTitle() try: # Saves channels to disk, # add more channels here if desired, # remember to define new counters. if "C=0" in projection: counter_C0 += 1 IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections"], "Scan" + str(counter_C0).zfill(3) + "C0")) IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections_C0"], "Scan" + str(counter_C0).zfill(3) + "C0")) elif "C=1" in projection: counter_C1 += 1 IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections"], "Scan" + str(counter_C1).zfill(3) + "C1")) IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections_C1"], "Scan" + str(counter_C1).zfill(3) + "C1")) elif "C=2" in projection: counter_C2 += 1 IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections"], "Scan" + str(counter_C2).zfill(3) + "C2")) IJ.saveAs(impout, "TIFF", os.path.join(dirs["Projections_C2"], "Scan" + str(counter_C2).zfill(3) + "C2")) except IOException: print "Directory does not exist" raise IJ.log("Images projected and saved to disk")