forked from sschube6/OLID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehandling.py
46 lines (34 loc) · 1.13 KB
/
filehandling.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
import javabridge
import bioformats
from bioformats import log4j
import numpy as np
from bioformats import omexml as ome
import os
def start_java_bridge():
javabridge.start_vm(class_path=bioformats.JARS)
log4j.basic_config()
def end_java_bridge():
javabridge.kill_vm()
def readfile(filename):
# read metadata
metadata = bioformats.get_omexml_metadata(filename)
xml = bioformats.OMEXML(metadata)
Pixels = xml.image().Pixels
nx, ny, nz, nt = Pixels.SizeX, Pixels.SizeY, Pixels.SizeZ, Pixels.SizeT
# read image data
image4d = np.zeros(shape=(nx, ny, nz, nt))
reader = bioformats.ImageReader(filename)
for t in range(nt):
for z in range(nz):
image4d[:, :, z, t] = reader.read(z=z, t=t, rescale=False)
return image4d
def writefile(filename, image):
# write image data
# if file exists already, we have to remove it to prevent java error of
# unmatching pixel dimensions
if os.path.isfile(filename):
os.remove(filename)
bioformats.write_image(
filename, image.astype(ome.PT_FLOAT), pixel_type=ome.PT_FLOAT)
if __name__ == '__main__':
main()