def save_diagnostic_image(ds_id: str, data: Any, key, index=None, fmt=DiagnosticImageFormat.NPY) -> DiagnosticImage: assert key in DiagnosticImageKey if fmt == DiagnosticImageFormat.NPY: image_id = save_npy_image(ds_id, data) elif fmt == DiagnosticImageFormat.JSON: image_id = image_storage.post_image( image_storage.DIAG, ds_id, BytesIO(numpy_json_dumps(data).encode())) elif fmt == DiagnosticImageFormat.PARQUET: image_id = save_parquet_image(ds_id, data) else: raise ValueError(f'Unknown format: {fmt}') image = DiagnosticImage( key=key, image_id=image_id, url=image_storage.get_image_url(image_storage.DIAG, ds_id, image_id), format=fmt, ) if index is not None: image['index'] = index return image
def _save_ion_thumbnail_image(ds_id, thumbnail): h, w, depth = thumbnail.shape fp = BytesIO() png_writer = png.Writer(width=w, height=h, alpha=True, compression=9) png_writer.write(fp, thumbnail.reshape(h, w * depth).tolist()) fp.seek(0) return image_storage.post_image(image_storage.THUMB, ds_id, fp.read())
def save_parquet_image(ds_id: str, data): assert isinstance( data, (pd.DataFrame, pd.Series)), 'Parquet data must be a DataFrame or Series' buf = BytesIO(data.to_parquet()) return image_storage.post_image(image_storage.DIAG, ds_id, buf)
def save_image(img): buf = _save_jpeg(img) scaled_img_id = image_storage.post_image(image_storage.OPTICAL, ds.id, buf.read()) scaled_img_url = image_storage.get_image_url(image_storage.OPTICAL, ds.id, scaled_img_id) return scaled_img_id, scaled_img_url
def test_post_get_image_success(): test_image_bytes = make_test_image_bytes() image_id = image_storage.post_image(image_storage.ISO, "ds-id", test_image_bytes) fetched_image_bytes = image_storage.get_image(image_storage.ISO, "ds-id", image_id) assert fetched_image_bytes == test_image_bytes
def test_delete_image_success(): test_image_bytes = make_test_image_bytes() image_id = image_storage.post_image(image_storage.ISO, "ds-id", test_image_bytes) image_storage.delete_image(image_storage.ISO, "ds-id", image_id) assert_no_image(image_id) # delete non-existing image should not raise exception image_storage.delete_image(image_storage.ISO, "ds-id", image_id)
def _add_thumbnail_optical_image(db, ds, dims, optical_img, transform): logger.debug(f'Saving optical image thumbnail: {optical_img}') thumbnail_size = (200, 200) db.alter(UPD_DATASET_THUMB_OPTICAL_IMAGE, params=(None, None, ds.id)) img = _transform_image_to_ion_space(optical_img, transform, dims, zoom=1)[0] img.thumbnail(thumbnail_size, Image.ANTIALIAS) buf = _save_jpeg(img) img_thumb_id = image_storage.post_image(image_storage.OPTICAL, ds.id, buf.read()) img_thumb_url = image_storage.get_image_url(image_storage.OPTICAL, ds.id, img_thumb_id) db.alter(UPD_DATASET_THUMB_OPTICAL_IMAGE, params=(img_thumb_id, img_thumb_url, ds.id))
def save_npy_image(ds_id: str, arr: np.ndarray): buf = BytesIO() np.save(buf, arr, allow_pickle=False) buf.seek(0) return image_storage.post_image(image_storage.DIAG, ds_id, buf)