-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeSTMmovie.py
executable file
·178 lines (158 loc) · 6.29 KB
/
makeSTMmovie.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import parseSTM as ps
import sys
import imageio
from PIL import Image
from skimage.filters import gaussian
from skimage.exposure import rescale_intensity
from numpy import percentile, array
from myImageToolset import normalize_img, imreg
import numpy as np
def natural_key(string_):
"""Short summary.
Parameters
----------
string_ : type
Description of parameter `string_`.
Returns
-------
type
Description of returned object.
"""
"""
See http://www.codinghorror.com/blog/archives/001018.html
This function is to define a key for sorting the filenames with numbers in it
"""
import re
return [int(s) if s.isdigit() else s for s in re.split(r"(\d+)", string_)]
def isImage(path):
if (
path.endswith("png")
or path.endswith("jpeg")
or path.endswith("jpg")
or path.endswith("tiff")
or path.endswith("tif")
or path.endswith("PNG")
or path.endswith("JPEG")
or path.endswith("JPG")
or path.endswith("TIFF")
or path.endswith("TIF")
):
return True
else:
return False
if __name__ == "__main__":
fpaths = []
for ind, arg in enumerate(sys.argv[1:]):
if "-h" == arg or "--help" == arg:
sys.exit("Syntax: makeSTMmovie [lydSTMfile1/ImageFile1] [lydSTMfile2/ImageFile2] ...")
fpaths.append(arg)
with open("log.txt", "w") as file:
np.savetxt(file, np.array(fpaths), delimiter=",", fmt="%s")
# USER INPUTS
smoothing = False
user_input = input("Apply a 1-pixel sigma gaussian filter? y/n ")
if user_input.lower() == "y":
smoothing = True
fps = 1
user_input = input("FPS? ")
try:
fps = float(user_input)
except ValueError:
print("FPS input is not a number, default to 1 FPS")
stretch_contrast = False
user_input = input("Apply contrast stretch (1%, 99%)? y/n ")
if user_input.lower() == "y":
stretch_contrast = True
save_image = False
user_input = input("Save image sequence? y/n ")
if user_input.lower() == "y":
save_image = True
# fpaths = sorted(fpaths, key=natural_key)
dirpath = fpaths[0].split(".")[0].split("/")[-1]
var = sum([isImage(path) for path in fpaths])
isSTM = False
isImg = True
if var == 0:
isSTM = True
isImg = False
elif var == len(fpaths):
isSTM = False
isImg = True
else:
sys.exit(status="File formats are not consistent. Use either a set of images or STM files")
image_set = []
if isSTM:
for path in fpaths:
dat = ps.STMfile(path)
image_set.append(dat.get_all_orig_buffers())
for buftype in image_set[0]:
to_write = [im[buftype] for im in image_set]
if smoothing:
to_write = [gaussian(im) for im in to_write]
if stretch_contrast:
to_write = [rescale_intensity(im, in_range=(percentile(im, 1), percentile(im, 99))) for im in to_write]
if buftype == 1:
imageio.mimwrite(f"{dirpath}.Topo_Trace.mov", to_write, fps=fps)
if save_image:
for i, im in enumerate(to_write):
num = fpaths[i].split(".")[-1]
imageio.imwrite(
f"{dirpath}.Topo_Trace_{num}.tif",
normalize_img(im * 10 ** 12, 0, 2 ** 16 - 1).astype("uint16"),
)
elif buftype == 2:
imageio.mimwrite(f"{dirpath}.Topo_Retrace.mov", to_write, fps=fps)
if save_image:
for i, im in enumerate(to_write):
num = fpaths[i].split(".")[-1]
imageio.imwrite(
f"{dirpath}.Topo_Retrace_{num}.tif",
normalize_img(im * 10 ** 12, 0, 2 ** 16 - 1).astype("uint16"),
)
elif buftype == 3:
imageio.mimwrite(f"{dirpath}.Current.mov", to_write, fps=fps)
if save_image:
for i, im in enumerate(to_write):
num = fpaths[i].split(".")[-1]
imageio.imwrite(
f"{dirpath}.Current_{num}.tif",
normalize_img(im * 10 ** 12, 0, 2 ** 16 - 1).astype("uint16"),
)
elif buftype == 4:
imageio.mimwrite(f"{dirpath}.LockInX.mov", array(to_write) * -1, fps=fps) # for colormap consistency
if save_image:
for i, im in enumerate(to_write):
num = fpaths[i].split(".")[-1]
imageio.imwrite(
f"{dirpath}.LockInX_{num}.tif",
normalize_img(array(im) * -1, 0, 2 ** 16 - 1).astype("uint16"),
)
elif buftype == 5:
imageio.mimwrite(f"{dirpath}.LockInY.mov", array(to_write) * -1, fps=fps)
if save_image:
for i, im in enumerate(to_write):
num = fpaths[i].split(".")[-1]
imageio.imwrite(
f"{dirpath}.LockInY_{num}.tif",
normalize_img(array(im) * -1, 0, 2 ** 16 - 1).astype("uint16"),
)
else:
print(f"I'm not sure what this buffer type {buftype} is. Skipping..")
continue
elif isImg:
for path in fpaths:
# dat = imageio.imread(path)
dat = np.array(Image.open(path))
image_set.append(dat)
to_write = image_set.copy()
# to_write = imreg(image_set, upsample_factor=1)
if smoothing:
to_write = [gaussian(im) for im in to_write]
if stretch_contrast:
to_write = [rescale_intensity(im, in_range=(percentile(im, 1), percentile(im, 99))) for im in to_write]
imageio.mimwrite(f"{dirpath}.movie.mov", to_write, fps=fps)
if save_image:
for i, im in enumerate(to_write):
imageio.imwrite(f"{dirpath}.movie_{i}.tif", normalize_img(im, 0, 2 ** 16 - 1).astype("uint16"))
else:
sys.exit(status="File formats are not consistent. Use either a set of images or STM files")