[ImageJ-devel] Save/write image with SCIFIO/BF in python script
Curtis Rueden
ctrueden at wisc.edu
Thu Jan 8 14:56:38 CST 2015
Hi Hadrien,
> I tried to use SCIFIO to write cropped image on disk but it doesn not
> work.
The error you see is because SCIFIO operates on ImgLib2 data structures,
not ImagePlus objects.
> Is there is any “easy” alternative to BF setCropRegion function in
> SCIFIO ?
Yes: you create a SCIFIOConfig, calling imgOpenerSetRegion [1] on it, then
pass it as an argument to the ImgOpener. You'll get back an ImgLib2 data
object which can then be fed to the SCIFIO ImgSaver.
I'm sorry that I don't have time to whip up an example for you right now.
It would be great to add more SCIFIO tutorials [2] that use the ImgOpener
and ImgSaver, since they are much higher level APIs akin to the Bio-Formats
"BF" functionality... please feel welcome to contribute some!
Regards,
Curtis
[1]
http://javadoc.imagej.net/SCIFIO/io/scif/config/SCIFIOConfig.html#imgOpenerSetRegion(io.scif.img.ImageRegion)
[2] https://github.com/scifio/scifio-tutorials
On Thu, Jan 8, 2015 at 2: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
>
> _______________________________________________
> ImageJ-devel mailing list
> ImageJ-devel at imagej.net
> http://imagej.net/mailman/listinfo/imagej-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://imagej.net/pipermail/imagej-devel/attachments/20150108/996c57b8/attachment.html>
More information about the ImageJ-devel
mailing list