[ImageJ-devel] Save/write image with SCIFIO/BF in python script

Hadrien Mary hadrien.mary at gmail.com
Thu Jan 8 14:22:40 CST 2015


I currently use setCropRegion option from BF.openImagePlus but I take
any other option to crop image according to a roi instance. I didn't
find how to do that in python.

--
Hadrien Mary



On Thu, Jan 8, 2015 at 9:18 PM, Hadrien Mary <hadrien.mary at gmail.com> wrote:
> Hi,
>
> I am writing a python macro which iterate over all rois in ROI Manager
> and then use setCropRegion function from bioformat plugin to open a
> cropped region of an image.
>
> I tried to use SCIFIO to write cropped image on disk but it doesn not work.
>
> (I am using an updated version of Fiji.)
>
> Here is my script:
>
> from ij.plugin.frame import RoiManager
> from ij import IJ
> from io.scif.img import ImgSaver
>
> from loci.plugins import BF
> from loci.plugins.in import ImporterOptions
> from loci.common import Region
>
> import os
>
> # Get current image filename
> imp = IJ.getImage()
> f = imp.getOriginalFileInfo()
> fname = os.path.join(f.directory, f.fileName)
>
> IJ.log('Image filename is %s' % fname)
>
> # Iterate over all ROIs from ROI Manager
> rois = RoiManager.getInstance().getRoisAsArray()
> for i, roi in enumerate(rois):
>
>     crop_id = i +1
>     IJ.log("Opening crop %i / %i" % (crop_id, len(rois)))
>
>     bounds = roi.getBounds()
>
>     x = bounds.x
>     y = bounds.y
>     w = bounds.width
>     h = bounds.height
>
>     # Import only cropped region of the image
>     options = ImporterOptions()
>     options.setCrop(True)
>     options.setCropRegion(0, Region(x, y, w, h))
>     options.setId(fname)
>     imps = BF.openImagePlus(options)
>
>     imp = imps[0]
>     imp.show()
>
>     crop_basename = "crop%i_%s" % (crop_id, f.fileName)
>     crop_fname = os.path.join(f.directory, crop_basename)
>     imp.setTitle(crop_basename)
>
>     # Save image
>     IJ.log("Saving crop to %s" % crop_fname)
>     saver = ImgSaver()
>     saver.saveImg(crop_basename, imp)
>
> IJ.log('Done')
>
> It fails with this error:
>
> Traceback (most recent call last):
>   File "/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py", line
> 49, in <module>
>     saver.saveImg(crop_basename, imp)
> TypeError: saveImg(): 1st arg can't be coerced to io.scif.Writer, String
>
> at org.python.core.Py.TypeError(Py.java:235)
> at org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:209)
> at org.python.core.PyReflectedFunction.throwBadArgError(PyReflectedFunction.java:312)
> at org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:321)
> at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:167)
> at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)
> at org.python.core.PyObject.__call__(PyObject.java:422)
> at org.python.core.PyObject.__call__(PyObject.java:426)
> at org.python.core.PyMethod.__call__(PyMethod.java:139)
> at org.python.pycode._pyx7.f$0(/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py:51)
> at org.python.pycode._pyx7.call_function(/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.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:175)
> 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:164)
> at java.util.concurrent.FutureTask.run(FutureTask.java:262)
> at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
> at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
> at java.lang.Thread.run(Thread.java:745)
>
> Three questions:
>
> how can I write cropped image (I need to write them as OME Tiff so I
> need BF or SCIFIO).
> Is there is any “easy” alternative to BF setCropRegion function in SCIFIO ?
> I am currently using RoiManager.getInstance().getRoisAsArray() to
> retrieve all rois. How can I directly get a ROIManager instance from
> RoiSet.zip file ?
>
> Any help would be very appreciated.
>
> Thanks !
>
>> Hadrien Mary



More information about the ImageJ-devel mailing list