def bin2zip(parsed_pid,canonical_pid,targets,hdr,timestamp,roi_path,outfile): """parsed_pid - result of parsing pid canonical_pid - canonicalized with URL prefix targets - list of (stitched) targets hdr - result of parsing header file timestamp - timestamp (FIXME in what format?) roi_path - path to ROI file outfile - where to write resulting zip file""" bin_lid = parsed_pid['bin_lid'] adc_cols = parsed_pid['adc_cols'].split(' ') targets = list(targets) with tempfile.SpooledTemporaryFile() as temp: z = ZipFile(temp,'w',ZIP_DEFLATED) csv_out = '\n'.join(targets2csv(targets, adc_cols))+'\n' z.writestr(bin_lid + '.csv', csv_out) xml_out = bin2xml(canonical_pid,hdr,targets,timestamp) z.writestr(bin_lid + '.xml', xml_out) with open(roi_path,'rb') as roi_file: for target in targets: if STITCHED in target and target[STITCHED] != 0: subRois = [read_target_image(t,file=roi_file) for t in target[PAIR]] im,_ = stitch(target[PAIR], subRois) else: im = read_target_image(target, file=roi_file) target_lid = os.path.basename(target['pid']) z.writestr(target_lid + '.png', as_bytes(im, mimetype='image/png')) z.close() temp.seek(0) shutil.copyfileobj(temp, outfile)
def get_target_image(parsed, target, path=None, file=None, raw_stitch=False): if PAIR in target: (a,b) = target[PAIR] a_image = read_target_image(a, path=path, file=file) b_image = read_target_image(b, path=path, file=file) if raw_stitch: return stitch_raw((a,b),(a_image,b_image),background=180) else: try: bin_zip = get_product_file(parsed, BINZIP_PRODUCT) if os.path.exists(bin_zip): # name is target LID + png extension png_name = os.path.basename(target[PID]) + '.png' png_data = get_zip_entry_bytes(bin_zip, png_name) pil_img = PIL.Image.open(StringIO(png_data)) return np.array(pil_img.convert('L')) # convert to 8-bit grayscale except NotFound: pass im,_ = v1_stitching.stitch((a,b),(a_image,b_image)) return im else: return read_target_image(target, path=path, file=file)