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 serve_blob_image(parsed, mimetype, outline=False, target_img=None): # first, read the blob from the zipfile blob_zip = get_product_file(parsed, 'blobs') png_name = parsed['lid'] + '.png' # name is target LID + png extension png_data = get_zip_entry_bytes(blob_zip, png_name) # if we want a blob png, then pass it through without conversion if not outline and mimetype == 'image/png': return Response(png_data, mimetype='image/png') else: # read the png-formatted blob image into a PIL image pil_img = PIL.Image.open(StringIO(png_data)) blob = np.array(pil_img.convert('L')) # convert to 8-bit grayscale if not outline: # unless we are drawing the outline return Response(as_bytes(blob), mimetype=mimetype) # format and serve else: blob_outline = find_boundaries(blob) roi = target_img blob = np.dstack([roi,roi,roi]) blob[blob_outline] = [255,0,0] return Response(as_bytes(blob, mimetype), mimetype=mimetype)
def serve_mosaic_image(time_series=None, pid=None, params='/'): """Generate a mosaic of ROIs from a sample bin. params include the following, with default values - series (mvco) - time series (FIXME: handle with resolver, clarify difference between namespace, time series, pid, lid) - size (1024x1024) - size of the mosaic image - page (1) - page. for typical image sizes the entire bin does not fit and so is split into pages. - scale - scaling factor for image dimensions """ # parse params size, scale, page = parse_mosaic_params(params) (w,h) = size parsed = parse_pid(pid) schema_version = parsed['schema_version'] try: paths = get_fileset(parsed) except NotFound: abort(404) adc_path = paths['adc_path'] roi_path = paths['roi_path'] bin_pid = canonicalize(get_url_root(), time_series, parsed['bin_lid']) # perform layout operation scaled_size = (int(w/scale), int(h/scale)) layout = list(get_mosaic_layout(adc_path, schema_version, bin_pid, scaled_size, page)) extension = parsed['extension'] # serve JSON on request if extension == 'json': return Response(json.dumps(list(layout2json(layout, scale))), mimetype=MIME_JSON) mimetype = mimetypes.types_map['.' + extension] # read all images needed for compositing and inject into Tiles image_layout = [] with open(roi_path,'rb') as roi_file: for tile in layout: target = tile.image # in mosaic API, the record is called 'image' # FIXME 1. replace PIL image = get_target_image(parsed, target, file=roi_file, raw_stitch=True) image_layout.append(Tile(PIL.Image.fromarray(image), tile.size, tile.position)) # produce and serve composite image mosaic_image = thumbnail(mosaic.composite(image_layout, scaled_size, mode='L', bgcolor=160), (w,h)) #pil_format = filename2format('foo.%s' % extension) return Response(as_bytes(mosaic_image), mimetype=mimetype)
def serve_pid(pid): req = DashboardRequest(pid, request) try: paths = get_fileset(req.parsed) except NotFound: abort(404) hdr_path = paths['hdr_path'] adc_path = paths['adc_path'] roi_path = paths['roi_path'] adc = Adc(adc_path, req.schema_version) heft = 'full' # heft is either short, medium, or full if 'extension' in req.parsed: extension = req.parsed['extension'] if 'target' in req.parsed: canonical_bin_pid = canonicalize(req.url_root, req.time_series, req.bin_lid) target_no = int(req.parsed['target']) # pull three targets, then find any stitched pair targets = adc.get_some_targets(target_no-1, 3) targets = list_stitched_targets(targets) for t in targets: if t[TARGET_NUMBER] == target_no: target = t add_pid(target, canonical_bin_pid) # check for image mimetype = mimetypes.types_map['.' + extension] if mimetype.startswith('image/'): if req.product=='raw': img = get_target_image(req.parsed, target, roi_path) return Response(as_bytes(img,mimetype),mimetype=mimetype) if req.product=='blob': return serve_blob_image(req.parsed, mimetype) if req.product=='blob_outline': img = get_target_image(req.parsed, target, roi_path) return serve_blob_image(req.parsed, mimetype, outline=True, target_img=img) # not an image, so get more metadata targets = get_targets(adc, canonical_bin_pid) # not an image, check for JSON if extension == 'json': return Response(json.dumps(target),mimetype=MIME_JSON) target = get_target_metadata(target,targets) # more metadata representations. we'll need the header hdr = parse_hdr_file(hdr_path) if extension == 'xml': return Response(target2xml(req.canonical_pid, target, req.timestamp, canonical_bin_pid), mimetype='text/xml') if extension == 'rdf': return Response(target2rdf(req.canonical_pid, target, req.timestamp, canonical_bin_pid), mimetype='text/xml') if extension in ['html', 'htm']: template = { 'static': STATIC, 'target_pid': req.canonical_pid, 'bin_pid': canonical_bin_pid, 'properties': target, 'target': target.items(), # FIXME use order_keys 'date': req.timestamp } return template_response('target.html',**template) else: # bin if req.extension in ['hdr', 'adc', 'roi']: path = dict(hdr=hdr_path, adc=adc_path, roi=roi_path)[req.extension] mimetype = dict(hdr='text/plain', adc='text/csv', roi='application/octet-stream')[req.extension] return Response(file(path,'rb'), direct_passthrough=True, mimetype=mimetype) try: if req.product in ['blobs','blob']: # accept old url pattern return serve_blob_bin(req.parsed) if req.product=='features': return serve_features_bin(req.parsed) if req.product=='class_scores': return serve_class_scores_bin(req.parsed) except NotFound: abort(404) # gonna need targets unless heft is medium or below targets = [] if req.product != 'short': targets = get_targets(adc, req.canonical_pid) # end of views # not a special view, handle representations of targets if req.extension=='csv': adc_cols = req.parsed[ADC_COLS].split(' ') lines = targets2csv(targets,adc_cols) return Response('\n'.join(lines)+'\n',mimetype='text/csv') # we'll need the header for the other representations hdr = parse_hdr_file(hdr_path) if req.extension in ['html', 'htm']: targets = list(targets) context, props = split_hdr(hdr) template = { 'static': STATIC, 'bin_pid': req.canonical_pid, 'time_series': req.time_series, 'context': context, 'properties': props, 'targets': targets, 'target_pids': [t['pid'] for t in targets], 'date': req.timestamp, 'files': get_files(req.parsed,check=True) # note: ORM call! } print get_files(req.parsed) return template_response('bin.html', **template) if req.extension=='json': if req.product=='short': return Response(bin2json_short(req.canonical_pid,hdr,req.timestamp),mimetype=MIME_JSON) if req.product=='medium': return Response(bin2json_medium(req.canonical_pid,hdr,targets,req.timestamp),mimetype=MIME_JSON) return Response(bin2json(req.canonical_pid,hdr,targets,req.timestamp),mimetype=MIME_JSON) if req.extension=='xml': return Response(bin2xml(req.canonical_pid,hdr,targets,req.timestamp),mimetype='text/xml') if req.extension=='rdf': return Response(bin2rdf(req.canonical_pid,hdr,targets,req.timestamp),mimetype='text/xml') if req.extension=='zip': # look to see if the zipfile is resolvable try: zip_path = get_product_file(req.parsed, 'binzip') if os.path.exists(zip_path): return Response(file(zip_path), direct_passthrough=True, mimetype='application/zip') except NotFound: pass except: raise buffer = BytesIO() bin2zip(req.parsed,req.canonical_pid,targets,hdr,req.timestamp,roi_path,buffer) return Response(buffer.getvalue(), mimetype='application/zip') return 'unimplemented'