def read_dm3_as_ndimage(filepath): """Read a .dm3 file.""" filename = os.path.split(filepath)[1] fileref = os.path.splitext(filename)[0] dm3f = dm3.DM3(filepath, debug=0) im = dm3f.imagedata return im, dm3f
def get_image(fpath, datatype): if fpath.endswith('.dm3'): dm3f = dm3.DM3(fpath, debug=0) im = dm3f.imagedata else: im = io.imread(fpath) return im.astype(datatype)
def dm3ToTiff(filename, newpath=getcwd()): dm3f = dm3.DM3(filename+".dm3", debug=0) A = dm3f.imagedata # get some useful tag data and print print "datatype:", dm3f.tags["root.ImageList.1.ImageData.DataType"] # A = rescale(A) # im = Image.fromarray(A, mode="I;16") # "I;16" is PIL's 16 bit unsigned int mode - do we need this for dm3 files? im = Image.fromarray(A) im.save(newpath+"/"+filename+".tif", format="TIFF") return
def get_metadata(files, datatype, outlayout, elsize): # derive the stop-values from the image data if not specified if files[0].endswith('.dm3'): try: import DM3lib as dm3 except ImportError: raise dm3f = dm3.DM3(files[0], debug=0) # yxdims = dm3f.imagedata.shape alt_dtype = dm3f.imagedata.dtype # yxelsize = dm3f.pxsize[0] id = 'root.ImageList.1.ImageData' tag = '{}.Dimensions.{:d}' yxdims = [] for dim in [0, 1]: yxdims += [int(dm3f.tags.get(tag.format(id, dim)))] tag = '{}.Calibrations.Dimension.{:d}.Scale' for lab, dim in zip('xy', [0, 1]): if elsize[outlayout.index(lab)] == -1: pxsize = float(dm3f.tags.get(tag.format(id, dim))) elsize[outlayout.index(lab)] = pxsize tag = 'root.ImageList.1.ImageTags.SBFSEM.Record.Slice thickness' slicethickness = float(dm3f.tags.get(tag.format(id, dim))) elsize[outlayout.index('z')] = slicethickness / 1000 else: yxdims = io.imread(files[0]).shape alt_dtype = io.imread(files[0]).dtype zyxdims = [len(files)] + list(yxdims) datatype = datatype or alt_dtype elsize = [0 if el is None else el for el in elsize] return zyxdims, datatype, elsize
def dm3ToPNG(filename, newpath=getcwd()): dm3f = dm3.DM3(filename + ".dm3", debug=0) A = dm3f.imagedata cuts = dm3f.cuts A_norm = A.copy() if cuts[0] != cuts[1]: A_norm[(A <= min(cuts))] = float(min(cuts)) A_norm[(A >= max(cuts))] = float(max(cuts)) # -- normalize A_norm = (A_norm - np.min(A_norm)) / (np.max(A_norm) - np.min(A_norm)) # -- scale to 0--255, convert to (8-bit) integer A_norm = np.uint8(np.round(A_norm * 255)) # - save as PNG and JPG im = Image.fromarray(A_norm) im.save(newpath + "/" + filename + ".png", format="PNG") return
def save_im(savedir, filepath, imformat='.jpg', dumptags=False): # get filename filename = os.path.split(filepath)[1] fileref = os.path.splitext(filename)[0] dm3f = dm3.DM3(filepath, debug=0) cuts = dm3f.cuts if dumptags: dm3f.dumpTags(savedir) aa = dm3f.imagedata # save image as TIFF if '.tif' in imformat: tif_file = os.path.join(savedir, fileref + imformat) im = Image.fromarray(aa) im.save(tif_file) # check TIFF dynamic range if Image.open(tif_file).mode == 'L': tif_range = "8-bit" else: tif_range = "32-bit" else: # - normalize image for conversion to 8-bit aa_norm = aa.copy() # -- apply cuts (optional) if cuts[0] != cuts[1]: aa_norm[ (aa <= min(cuts)) ] = float(min(cuts)) aa_norm[ (aa >= max(cuts)) ] = float(max(cuts)) # -- normalize aa_norm = (aa_norm - np.min(aa_norm)) / (np.max(aa_norm) - np.min(aa_norm)) # -- scale to 0--255, convert to (8-bit) integer aa_norm = np.uint8(np.round( aa_norm * 255 )) # - save as <imformat> im_dsp = Image.fromarray(aa_norm) im_dsp.save(os.path.join(savedir, fileref + imformat))
# parse command line arguments args = parser.parse_args() if args.verbose: debug = 1 filepath = args.file # get filename filename = os.path.split(filepath)[1] fileref = os.path.splitext(filename)[0] # pyplot interactive mode plt.ion() plt.close('all') # parse DM3 file dm3f = dm3.DM3(filepath, debug=debug) # get some useful tag data and print print "file:", dm3f.filename print "file info.:" print dm3f.info print "scale: %.3g %s/px" % dm3f.pxsize cuts = dm3f.cuts print "cuts:", cuts # dump image Tags in txt file if args.dump: dm3f.dumpTags(savedir) # get image data aa = dm3f.imagedata