-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_images_and_masks_to_omezarr.py
105 lines (87 loc) · 3.16 KB
/
convert_images_and_masks_to_omezarr.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import click
import numpy as np
import zarr
import pathlib
from ome_zarr.io import parse_url
from ome_zarr.writer import write_image
from aicsimageio import AICSImage
def convert_image_to_zarr(in_impath, out_filepath):
im = AICSImage(in_impath)
data = im.data
# distinguish if the image is RGB or grey scale
ax = im.dims.order
if "S" in ax:
newdata=np.transpose(data, (0,5,2,3,4,1)) # reshape to work with ome-zarr specification
data = np.squeeze(newdata, axis=5)
ax= "TCZYX"
mode = "color"
channel_colors =["FF0000","00FF00","0000FF"]
else:
mode = "greyscale"
channel_colors = ["808080"] * data.shape[1]
# write the image
store = parse_url(out_filepath.with_suffix(".zarr"), mode="w").store
root = zarr.group(store=store)
write_image(image=data, group=root, axes=ax.lower())
# compute max and min pixel values for each channel
smallest = np.min(data, axis=(0,2,3,4))
largest = np.max(data, axis=(0,2,3,4))
# generate omero metadata with rendering settings
ch = []
for i in range(data.shape[1]):
ch.append(
{
"active": True,
"coefficient": 1,
"color": channel_colors[i],
"family": "linear",
"inverted": False,
"label": "Channel "+str(i),
"window": {
"end": largest[i],
"max": largest[i],
"min": smallest[i],
"start": smallest[i],
},
}
)
ome_metadata = {
"channels": ch,
"rdefs": {
"defaultT": 0,
"defaultZ": 0,
"model": mode,
},
}
root.attrs["omero"] = ome_metadata
return root
@click.command()
@click.argument("input_impath", type=click.Path(exists=True, path_type=pathlib.Path))
@click.argument("input_maskpath", type=click.Path(exists=True, path_type=pathlib.Path),required = False)
@click.option("-o", "--output_dirpath", type=click.Path(),
help="Output zarr file to this path; Defaults to " +
"the input image path")
def main(input_impath, input_maskpath, output_dirpath):
if output_dirpath:
output_dirpath = pathlib.Path(output_dirpath)
output_dirpath.mkdir(exist_ok=True, parents=True)
output_filepath = output_dirpath/input_impath.name
else:
output_filepath = input_impath
if input_maskpath:
root = convert_image_to_zarr(input_impath, output_filepath)
# write the mask to the labels folder
mask = AICSImage(input_maskpath)
label = mask.data
axl = mask.dims.order
labels_grp = root.create_group("labels")
# create the necessary .zattrs at each level
label_name = "mask"
labels_grp.attrs["labels"] = [label_name]
label_grp = labels_grp.create_group(label_name)
label_grp.attrs["image-label"] = {}
write_image(label, label_grp, axes=axl.lower())
else:
convert_image_to_zarr(input_impath, output_filepath)
if __name__ == "__main__":
main()