forked from oist/optinist
-
Notifications
You must be signed in to change notification settings - Fork 3
/
image.py
68 lines (57 loc) · 2.02 KB
/
image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gc
from typing import Optional
import imageio
import numpy as np
import tifffile
from studio.app.common.core.utils.filepath_creater import (
create_directory,
join_filepath,
)
from studio.app.common.core.utils.json_writer import JsonWriter
from studio.app.common.core.workflow.workflow import OutputPath, OutputType
from studio.app.common.dataclass.base import BaseData
from studio.app.common.dataclass.utils import create_images_list
from studio.app.common.schemas.outputs import PlotMetaData
from studio.app.dir_path import DIRPATH
class ImageData(BaseData):
def __init__(
self,
data,
output_dir=DIRPATH.OUTPUT_DIR,
file_name="image",
meta: Optional[PlotMetaData] = None,
):
super().__init__(file_name)
self.json_path = None
self.meta = meta
if data is None:
self.path = None
elif isinstance(data, str):
self.path = data
elif isinstance(data, list) and isinstance(data[0], str):
self.path = data
else:
_dir = join_filepath([output_dir, "tiff", file_name])
create_directory(_dir)
_path = join_filepath([_dir, f"{file_name}.tif"])
tifffile.imsave(_path, data)
self.path = [_path]
del data
gc.collect()
@property
def data(self):
if isinstance(self.path, list):
return np.concatenate([imageio.volread(p) for p in self.path])
else:
return np.array(imageio.volread(self.path))
def save_json(self, json_dir):
self.json_path = join_filepath([json_dir, f"{self.file_name}.json"])
JsonWriter.write_as_split(self.json_path, create_images_list(self.data))
JsonWriter.write_plot_meta(json_dir, self.file_name, self.meta)
@property
def output_path(self) -> OutputPath:
return OutputPath(
path=self.json_path,
type=OutputType.IMAGE,
max_index=len(self.data) if self.data.ndim == 3 else 1,
)