def test_save_preview_image(self): img = previewimage.get_preview_image(self.dv) out_path = os.path.join(tempfile.gettempdir(), "temp.png") previewimage.save_image(img, out_path, overwrite=True) img2 = imread(out_path) np.testing.assert_array_equal(img, img2)
def get_meta(input_file_path, output_path, **kwargs): """ Extract specific metadata typically used in bio-image analysis. Also outputs a preview image to the output directory. Parameters ---------- input_file_path: str Input file path output_path: str Returns ------- meta: [dict] List of dicts containing with keys and values for specific metadata """ pix_exc = set(["id", "significantbits", "bigendian", "interleaved"]) channel_exc = set(["color", "id", "color", "contrastmethod", "fluor", "ndfilter", "illuminationtype", "name", "pockelcellsetting", "acquisitionmode"]) input_fname, ext = os.path.splitext(os.path.basename(input_file_path)) if ext[1:] not in bioformats.READABLE_FORMATS: logger.debug("Unsupported format: %s.%s" % (input_fname, ext)) return omexml = bioformats.get_omexml_metadata(input_file_path).encode('utf-8') meta_xml = et.fromstring(omexml) meta = list() for i, img_meta in enumerate(meta_xml.iter(IMAGE)): smeta = dict() output_file_path = os.path.join(output_path, input_fname+"_s%s.png" % i) logger.debug("Generating series %s preview from image: %s" % (i, input_fname+ext)) img = previewimage.get_preview_image(input_file_path, omexml, series=i) logger.debug("Saving series %s preview from image: %s" % (i, input_fname+ext)) previewimage.save_image(img, output_file_path, overwrite=True) logger.debug("Extracting metadata for series %s preview from image: %s" % (i, input_fname+ext)) smeta['id'] = img_meta.attrib['ID'] smeta['name'] = img_meta.attrib['Name'] smeta['previewImage'] = output_file_path for pix_meta in img_meta.iter(PIXEL): for k, v in pix_meta.attrib.iteritems(): if k.lower() not in pix_exc: smeta[k.lower()] = v for c, channel_meta in enumerate(pix_meta.iter(CHANNEL)): for kc, vc in channel_meta.attrib.iteritems(): if kc.lower() not in channel_exc: if kc.lower() not in smeta: smeta[kc.lower()] = ["Channel %s: %s" % (c, vc)] else: smeta[kc.lower()].append("Channel %s: %s" % (c, vc)) meta.append(smeta) return meta
def get_meta(input_file_path, output_path, **kwargs): """ Extract specific metadata typically used in bio-image analysis. Also outputs a preview image to the output directory. Parameters ---------- input_file_path: str Input file path output_path: str Returns ------- meta: [dict] List of dicts containing with keys and values for specific metadata """ pix_exc = set(["id", "significantbits", "bigendian", "interleaved"]) channel_exc = set(["color", "id", "color", "contrastmethod", "fluor", "ndfilter", "illuminationtype", "name", "pockelcellsetting", "acquisitionmode"]) input_fname, ext = os.path.splitext(os.path.basename(input_file_path)) if ext[1:] not in bioformats.READABLE_FORMATS: logger.debug("Unsupported format: %s.%s" % (input_fname, ext)) return try: omexml = bioformats.get_omexml_metadata(input_file_path)\ .encode('utf-8') except javabridge.jutil.JavaException: logger.error("Unable to read OME Metadata from: %s", input_file_path) return meta_xml = et.fromstring(omexml) ome_ns = get_namespaces(meta_xml) meta = list() for i, img_meta in enumerate(meta_xml.findall('ome:Image', ome_ns)): smeta = dict() output_file_path = os.path.join(output_path, input_fname+"_s%s.png" % i) logger.debug("Generating series %s preview from image: %s", i, input_file_path) img = previewimage.get_preview_image(input_file_path, omexml, series=i) logger.debug("Saving series %s preview from image: %s", i, input_file_path) previewimage.save_image(img, output_file_path, overwrite=True) logger.debug("Extracting metadata for series %s preview from image: %s", i, input_file_path) smeta['id'] = img_meta.attrib['ID'] smeta['name'] = img_meta.attrib['Name'] smeta['previewImage'] = output_file_path for pix_meta in img_meta.findall('ome:Pixels', ome_ns): for k, v in pix_meta.attrib.iteritems(): if k.lower() not in pix_exc: smeta[k.lower()] = v for c, channel_meta in enumerate(pix_meta.findall('ome:Channel', ome_ns)): for kc, vc in channel_meta.attrib.iteritems(): if kc.lower() not in channel_exc: if kc.lower() not in smeta: smeta[kc.lower()] = ["Channel %s: %s" % (c, vc)] else: smeta[kc.lower()].append("Channel %s: %s" % (c, vc)) meta.append(smeta) return meta