I’m trying to write a Jython script to combine individual images into a single TIFF hyperstack.
They are named as following : prefix_Wchannel_Zslice.tif
I’m able to achieve this by opening images in the right order, then using first images to stack and then stack to hyperstack.
I’ve come up with this script (should work for one defined prefix):
import os
from ij.io import DirectoryChooser, FileSaver
from ij import IJ, ImagePlus, VirtualStack
from ij.plugin import HyperStackConverter
def run():
listimg = [0 for x in range(34)]
srcDir = '/path/to/images/'
for root, directories, filenames in os.walk(srcDir):
for filename in filenames:
if filename.startswith(prefix):
listimg[order[filename[len(prefix):-12]]] = filename
vs = None
for filename in listimg:
imp = IJ.openImage(srcDir+filename)
if not vs:
vs = VirtualStack(imp.width, imp.height, None, srcDir)
vs.addSlice(filename)
hs = ImagePlus(prefix[:-1], vs)
vhs = HyperStackConverter.toHyperStack(hs,2,17,1)
vhs.show()
fs = FileSaver(vhs)
fs.saveAsTiff("/where/i/want/the/output/test.tif")
prefix='prefix'
order={}
for i,name in enumerate(['W'+str(W)+'_Z'+str(Z) for Z in range(1,18)
for W in range(1,3)]):
order[name]=i
run()
I have 2 problems with this really basic script:
-
Unlike when I use the menu items, I can see both channels at the same time on the output image (switching between the 2 c doesn’t change anything)
-
Trying to save the image as I did (with or without displaying it first) results in:
Traceback (most recent call last): File "/home/ncedilni/Dropbox/rnpdetect/scripts/fiji/hyperstackimages.py", line 31, in <module> order[name]=i File "/home/ncedilni/Dropbox/rnpdetect/scripts/fiji/hyperstackimages.py", line 23, in run # fs.save() at ij.io.FileSaver.saveAsTiffStack(FileSaver.java:178) at ij.io.FileSaver.saveAsTiff(FileSaver.java:99) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) java.lang.NullPointerException: java.lang.NullPointerException at org.python.core.Py.JavaError(Py.java:495) at org.python.core.Py.JavaError(Py.java:488) at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:188) at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204) at org.python.core.PyObject.__call__(PyObject.java:404) at org.python.core.PyObject.__call__(PyObject.java:408) at org.python.core.PyMethod.__call__(PyMethod.java:124) at org.python.pycode._pyx4.run$1(/home/ncedilni/Dropbox/rnpdetect/scripts/fiji/hyperstackimages.py:23) at org.python.pycode._pyx4.call_function(/home/ncedilni/Dropbox/rnpdetect/scripts/fiji/hyperstackimages.py) at org.python.core.PyTableCode.call(PyTableCode.java:165) at org.python.core.PyBaseCode.call(PyBaseCode.java:120) at org.python.core.PyFunction.__call__(PyFunction.java:307) at org.python.pycode._pyx4.f$0(/home/ncedilni/Dropbox/rnpdetect/scripts/fiji/hyperstackimages.py:31) at org.python.pycode._pyx4.call_function(/home/ncedilni/Dropbox/rnpdetect/scripts/fiji/hyperstackimages.py) at org.python.core.PyTableCode.call(PyTableCode.java:165) at org.python.core.PyCode.call(PyCode.java:18) at org.python.core.Py.runCode(Py.java:1275) at org.scijava.plugins.scripting.jython.JythonScriptEngine.eval(JythonScriptEngine.java:76) at org.scijava.script.ScriptModule.run(ScriptModule.java:174) at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167) at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126) at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65) at org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:191) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745) Caused by: java.lang.NullPointerException at ij.io.FileSaver.saveAsTiffStack(FileSaver.java:178) at ij.io.FileSaver.saveAsTiff(FileSaver.java:99) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186) ... 24 more
Should I use other classes ?
What am I doing wrong ?