-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompressTiff.py
158 lines (100 loc) · 4.33 KB
/
compressTiff.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python3
# by Facundo Sosa-Rey, 2021. MIT license
from tifffile import TiffFile
import tifffile
import numpy as np
from extractCenterPoints import getTiffProperties
import os
import sys
if len(sys.argv)<2:
raise RuntimeError("need to pass first argument as folderPath")
commonPath=sys.argv[1]
if __name__ == "__main__":
print("commonPath: \n"+commonPath)
filesInDir = [f.path for f in os.scandir(commonPath) if f.is_file()]
indexPerim=None
indexHist =None
indexFibers=None
indexPores=None
for i,iPath in enumerate(filesInDir):
if ".tiff" in iPath:
if "V_hist.tiff" in iPath:
indexHist=i
if "V_perim.tiff" in iPath:
indexPerim=i
if "V_pores.tiff" in iPath:
indexPores=i
########################################################################################
print("\tprocessing V_fibers.tiff")
with TiffFile(os.path.join(commonPath,"V_fibers.tiff")) as tif:
xRes,unitTiff=getTiffProperties(tif)
V_fibers=np.array(tif.asarray())
descriptionStr="{"+"\"shape([x,y,z])\":[{},{},{}]".format(V_fibers.shape[1],V_fibers.shape[2],V_fibers.shape[0])+"}"
tifffile.imwrite(
os.path.join(commonPath,"V_fibers.tiff"),
V_fibers,
resolution=(xRes,xRes,unitTiff), #yRes==xRes
compress=True,
description=descriptionStr
)
print("\tprocessing V_fibers.tiff completed")
########################################################################################
print("\tprocessing V_hist.tiff")
if indexHist is not None:
with TiffFile(os.path.join(commonPath,"V_hist.tiff")) as tif:
xRes,unitTiff=getTiffProperties(tif)
V_hist=np.array(tif.asarray())
descriptionStr="{"+"\"shape([x,y,z])\":[{},{},{}]".format(V_hist.shape[1],V_hist.shape[2],V_hist.shape[0])+"}"
print("\tdescription: "+descriptionStr)
tifffile.imwrite(
os.path.join(commonPath,"V_hist.tiff"),
V_hist,
resolution=(xRes,xRes,unitTiff), #yRes==xRes
compress=True,
description=descriptionStr
)
print("\tprocessing V_hist.tiff completed")
########################################################################################
#Perim will not be present if not created in preprocessing
if indexPerim is not None:
print("\tprocessing V_perim.tiff")
with TiffFile(os.path.join(commonPath,"V_perim.tiff")) as tif:
xRes,unitTiff=getTiffProperties(tif)
V_perim=np.array(tif.asarray())
descriptionStr="{"+"\"shape([x,y,z])\":[{},{},{}]".format(V_perim.shape[1],V_perim.shape[2],V_perim.shape[0])+"}"
tifffile.imwrite(
os.path.join(commonPath,"V_perim.tiff"),
V_perim,
resolution=(xRes,xRes,unitTiff), #yRes==xRes
compress=True,
description=descriptionStr
)
print("\tprocessing V_perim.tiff completed")
########################################################################################
print("\tprocessing V_pores.tiff")
with TiffFile(os.path.join(commonPath,"V_pores.tiff")) as tif:
xRes,unitTiff=getTiffProperties(tif)
V_pores=np.array(tif.asarray())
descriptionStr="{"+"\"shape([x,y,z])\":[{},{},{}]".format(V_pores.shape[1],V_pores.shape[2],V_pores.shape[0])+"}"
tifffile.imwrite(
os.path.join(commonPath,"V_pores.tiff"),
V_pores,
resolution=(xRes,xRes,unitTiff), #yRes==xRes
compress=True,
description=descriptionStr
)
print("\tprocessing V_pores.tiff completed")
########################################################################################
print("\tprocessing V_prob.tiff")
with TiffFile(os.path.join(commonPath,"V_prob.tiff")) as tif:
xRes,unitTiff=getTiffProperties(tif)
V_prob=np.array(tif.asarray())
descriptionStr="{"+"\"shape([x,y,z])\":[{},{},{}]".format(V_prob.shape[1],V_prob.shape[2],V_prob.shape[0])+"}"
tifffile.imwrite(
os.path.join(commonPath,"V_prob.tiff"),
V_prob,
resolution=(xRes,xRes,unitTiff), #yRes==xRes
compress=True,
description=descriptionStr
)
print("\tprocessing V_prob.tiff completed")