-
Notifications
You must be signed in to change notification settings - Fork 0
/
images_to_frames_stack.py
53 lines (46 loc) · 1.82 KB
/
images_to_frames_stack.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
#@File (label="Images folder", style="directory") imgdir
#@String (label="Images extension", value=".tif") ext
#@Boolean (label="Save the stack?") save
#@String (visibility=MESSAGE, value="Select save if you want to keep to stack containing the images to be traced on disk.") msg
# Bootstrap to extend modules search path #
from sys import path
import os.path
from java.lang.System import getProperty
jython_scripts = os.path.join(getProperty('user.home'), 'Jython_scripts')
path.append(jython_scripts)
#=========================================#
from IBPlib.ij.Utils.Files import buildList
from IBPlib.ij.Utils import ThreadedFileSaver
from ij import (IJ, ImagePlus, ImageStack)
from ij.plugin import ZProjector
def images_to_frames_stack(imgdir, savestack=False):
'''
Method for preparing a stack of frames using images inside the input folder.
When savestack is true it saves the resulting stack to the images folder while opening the stack.
'''
title = "tracing_stack.tiff" # Title of the final image stack.
imgdir = imgdir.getPath()
imglist = buildList(imgdir, ext)
if not imglist:
raise IOError("No {0} were found in {0}.".format(ext, imgdir))
method = "max"
implist = []
for img in imglist:
imp = ImagePlus(img)
if imp.isStack():
imp = ZProjector.run(imp, method)
implist.append(imp)
stack = ImageStack(
implist[0].getWidth(),
implist[0].getHeight())
[stack.addSlice(img.getProcessor()) for img in implist]
stackimp = ImagePlus(title, stack)
IJ.run(stackimp, "Properties...", "slices=1 frames={0}".format(stackimp.getStackSize()))
if savestack:
IJ.log("# Saving tracing stack...")
save_string = os.path.join(imgdir, title)
fs = ThreadedFileSaver.ThreadedFileSaver(stackimp, save_string, "saveAsTiff")
fs.start()
stackimp.show()
if __name__ in ("__builtin__", "__main__"):
images_to_frames_stack(imgdir, save)