def _add_raw_optical_image(db, ds, img_id, transform): logger.debug(f'Saving raw optical image info: {img_id}, {transform}') row = db.select_one(SEL_DATASET_RAW_OPTICAL_IMAGE, params=(ds.id, )) if row: old_img_id = row[0] if old_img_id and old_img_id != img_id: image_storage.delete_image(image_storage.OPTICAL, ds.id, old_img_id) db.alter(UPD_DATASET_RAW_OPTICAL_IMAGE, params=(img_id, transform, ds.id))
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_zoom_optical_images(db, ds, dims, optical_img, transform, zoom_levels): logger.debug(f'Saving zoom optical images: {optical_img}') 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 rows = [] for zoom in zoom_levels: img, (width, height), transform_to_ion_space = _scale_image( optical_img, transform, zoom) scaled_img_id, scaled_img_url = save_image(img) rows.append(( scaled_img_id, ds.id, OpticalImageType.SCALED, zoom, width, height, transform_to_ion_space, scaled_img_url, )) img, (width, height), transform_to_ion_space = _transform_image_to_ion_space( optical_img, transform, dims, zoom) scaled_img_id, scaled_img_url = save_image(img) rows.append(( scaled_img_id, ds.id, OpticalImageType.CLIPPED_TO_ION_IMAGE, zoom, width, height, transform_to_ion_space, scaled_img_url, )) for img_id in db.select_onecol(SEL_OPTICAL_IMAGE, params=(ds.id, )): image_storage.delete_image(image_storage.OPTICAL, ds.id, img_id) db.alter(DEL_OPTICAL_IMAGE, params=(ds.id, )) db.insert(INS_OPTICAL_IMAGE, rows=rows)
def generate_ion_thumbnail_lithops( executor: Executor, db, ds: Dataset, only_if_needed=False, algorithm=DEFAULT_ALGORITHM, ): try: (existing_thumb_id,) = db.select_one(THUMB_SEL, [ds.id]) if existing_thumb_id and only_if_needed: return annotation_rows = db.select(ISO_IMAGE_SEL, [ds.id]) if not annotation_rows: logger.warning('Could not create ion thumbnail - no annotations found') return ds_id = ds.id sm_config = SMConfig.get_conf() def generate(annotation_rows): return _generate_ion_thumbnail_image( image_storage.ImageStorage(sm_config), ds_id, annotation_rows, algorithm ) thumbnail = executor.call( generate, (annotation_rows,), runtime_memory=2048, include_modules=['png'] ) image_id = _save_ion_thumbnail_image(ds.id, thumbnail) image_url = image_storage.get_image_url(image_storage.THUMB, ds.id, image_id) db.alter(THUMB_UPD, [image_id, image_url, ds.id]) if existing_thumb_id: image_storage.delete_image(image_storage.THUMB, ds.id, existing_thumb_id) except Exception: logger.error('Error generating ion thumbnail image', exc_info=True)
def generate_ion_thumbnail(db, ds, only_if_needed=False, algorithm=DEFAULT_ALGORITHM): try: (existing_thumb_id,) = db.select_one(THUMB_SEL, [ds.id]) if existing_thumb_id and only_if_needed: return annotation_rows = db.select(ISO_IMAGE_SEL, [ds.id]) if not annotation_rows: logger.warning('Could not create ion thumbnail - no annotations found') return thumbnail = _generate_ion_thumbnail_image(image_storage, ds.id, annotation_rows, algorithm) image_id = _save_ion_thumbnail_image(ds.id, thumbnail) image_url = image_storage.get_image_url(image_storage.THUMB, ds.id, image_id) db.alter(THUMB_UPD, [image_id, image_url, ds.id]) if existing_thumb_id: image_storage.delete_image(image_storage.THUMB, ds.id, existing_thumb_id) except Exception: logger.error('Error generating ion thumbnail image', exc_info=True)
def del_optical_image(db, ds_id): """Delete raw and zoomed optical images from DB and FS.""" ds = Dataset.load(db, ds_id) logger.info(f'Deleting optical image of "{ds.id}" dataset') (raw_img_id, ) = db.select_one(SEL_DATASET_RAW_OPTICAL_IMAGE, params=(ds.id, )) if raw_img_id: image_storage.delete_image(image_storage.OPTICAL, ds_id, raw_img_id) for img_id in db.select_onecol(SEL_OPTICAL_IMAGE, params=(ds.id, )): image_storage.delete_image(image_storage.OPTICAL, ds_id, img_id) (thumbnail_img_id, ) = db.select_one(SEL_OPTICAL_IMAGE_THUMBNAIL, params=(ds.id, )) if thumbnail_img_id: image_storage.delete_image(image_storage.OPTICAL, ds_id, thumbnail_img_id) db.alter(DEL_DATASET_RAW_OPTICAL_IMAGE, params=(ds.id, )) db.alter(DEL_OPTICAL_IMAGE, params=(ds.id, )) db.alter(UPD_DATASET_THUMB_OPTICAL_IMAGE, params=(None, None, ds.id))
def delete_ion_thumbnail(db, ds: Dataset): (thumb_id,) = db.select_one(THUMB_SEL, [ds.id]) if thumb_id: image_storage.delete_image(image_storage.THUMB, ds.id, thumb_id)