def save_mha_files(request, img): # Save MHA files to Image.files model img_file_model = ImageFile(image=img) # Save image fieldfile into ImageFile, also triggers ImageFile model save method image_file = File(request.data["image_mha"]) image_name = "out.mha" img_file_model.file.save(image_name, image_file, save=True)
def _create_image_file(*, path: str, image: Image) -> ImageFile: temp_file = TemporaryFile() with open(path, "rb") as open_file: buffer = True while buffer: buffer = open_file.read(1024) temp_file.write(buffer) if path.lower().endswith("dzi"): return ImageFile( image=image, image_type=ImageFile.IMAGE_TYPE_DZI, file=File(temp_file, name=f"{image.pk}.dzi"), ) else: return ImageFile( image=image, image_type=ImageFile.IMAGE_TYPE_TIFF, file=File(temp_file, name=f"{image.pk}.tif"), )
def save_mhd_and_raw_files(self, request, img): # Save MHD and ZRAW files to Image.files model file_name = "out" # Set ElementDataFile in mhd file to correct zraw filename raw_file_name = file_name + ".zraw" request.data["image_hd"] = self.set_element_data_file_header( request.data["image_hd"], raw_file_name) # Save mhd and zraw files for image_key in ("image_hd", "image_raw"): img_file_model = ImageFile(image=img) # Save image fieldfile into ImageFile, also triggers ImageFile model save method image_file = File(request.data[image_key]) extension = "zraw" if image_key == "image_raw" else "mhd" image_name = f"{file_name}.{extension}" img_file_model.file.save(image_name, image_file, save=True)
def convert_itk_file(headers: Mapping[str, Union[str, None]], filename: Path) -> Tuple[Image, Sequence[ImageFile]]: try: simple_itk_image = sitk.ReadImage(str(filename.absolute())) simple_itk_image: sitk.Image except RuntimeError: raise ValueError("SimpleITK cannot open file") color_space = simple_itk_image.GetNumberOfComponentsPerPixel() color_space = { 1: Image.COLOR_SPACE_GRAY, 3: Image.COLOR_SPACE_RGB, 4: Image.COLOR_SPACE_RGBA, }.get(color_space, None) if color_space is None: raise ValueError("Unknown color space for MetaIO image.") with TemporaryDirectory() as work_dir: work_dir = Path(work_dir) sitk.WriteImage(simple_itk_image, str(work_dir / "out.mhd"), True) depth = simple_itk_image.GetDepth() db_image = Image( name=filename.name, width=simple_itk_image.GetWidth(), height=simple_itk_image.GetHeight(), depth=depth if depth else None, resolution_levels=None, color_space=color_space, ) db_image_files = [] for _file in work_dir.iterdir(): temp_file = TemporaryFile() with open(_file, "rb") as open_file: buffer = True while buffer: buffer = open_file.read(1024) temp_file.write(buffer) db_image_file = ImageFile( image=db_image, image_type=ImageFile.IMAGE_TYPE_MHD, file=File(temp_file, name=_file.name), ) db_image_files.append(db_image_file) return db_image, db_image_files
def _convert_panimg_to_django(*, panimg_result: PanImgResult) -> ConversionResult: new_images = {Image(**asdict(im)) for im in panimg_result.new_images} new_image_files = { ImageFile( image_id=f.image_id, image_type=f.image_type, file=File(open(f.file, "rb"), f.file.name), ) for f in panimg_result.new_image_files } new_folders = { FolderUpload(**asdict(f)) for f in panimg_result.new_folders } return ConversionResult( new_images=new_images, new_image_files=new_image_files, new_folders=new_folders, )
def image_builder_tiff(path: Path) -> ImageBuilderResult: new_images = [] new_image_files = [] consumed_files = set() invalid_file_errors = {} for file_path in path.iterdir(): try: tiff_file = load_tiff_file(path=file_path) except ValidationError as e: invalid_file_errors[file_path.name] = e.message continue image = create_tiff_image_entry(tiff_file=tiff_file) temp_file = TemporaryFile() with open(tiff_file.path.absolute(), "rb") as open_file: buffer = True while buffer: buffer = open_file.read(1024) temp_file.write(buffer) new_image_files.append( ImageFile( image=image, image_type=ImageFile.IMAGE_TYPE_TIFF, file=File(temp_file, name="out.tif"), ) ) new_images.append(image) consumed_files.add(tiff_file.path.name) return ImageBuilderResult( consumed_files=consumed_files, file_errors_map=invalid_file_errors, new_images=new_images, new_image_files=new_image_files, )