<div dir="ltr">Hi Hadrien,<div><br></div><div><div>> I tried to use SCIFIO to write cropped image on disk but it doesn not</div><div>> work.</div></div><div><br></div><div>The error you see is because SCIFIO operates on ImgLib2 data structures, not ImagePlus objects.<br></div><div><div><br></div></div><div><div>> Is there is any “easy” alternative to BF setCropRegion function in</div><div>> SCIFIO ?</div></div><div><br></div><div>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.</div><div><br></div><div>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!</div><div><br></div><div>Regards,</div><div>Curtis</div><div><br></div><div>[1] <a href="http://javadoc.imagej.net/SCIFIO/io/scif/config/SCIFIOConfig.html#imgOpenerSetRegion(io.scif.img.ImageRegion)">http://javadoc.imagej.net/SCIFIO/io/scif/config/SCIFIOConfig.html#imgOpenerSetRegion(io.scif.img.ImageRegion)</a></div><div>[2] <a href="https://github.com/scifio/scifio-tutorials">https://github.com/scifio/scifio-tutorials</a></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 8, 2015 at 2:18 PM, Hadrien Mary <span dir="ltr"><<a href="mailto:hadrien.mary@gmail.com" target="_blank">hadrien.mary@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I am writing a python macro which iterate over all rois in ROI Manager<br>
and then use setCropRegion function from bioformat plugin to open a<br>
cropped region of an image.<br>
<br>
I tried to use SCIFIO to write cropped image on disk but it doesn not work.<br>
<br>
(I am using an updated version of Fiji.)<br>
<br>
Here is my script:<br>
<br>
from ij.plugin.frame import RoiManager<br>
from ij import IJ<br>
from io.scif.img import ImgSaver<br>
<br>
from loci.plugins import BF<br>
from <a href="http://loci.plugins.in" target="_blank">loci.plugins.in</a> import ImporterOptions<br>
from loci.common import Region<br>
<br>
import os<br>
<br>
# Get current image filename<br>
imp = IJ.getImage()<br>
f = imp.getOriginalFileInfo()<br>
fname = os.path.join(f.directory, f.fileName)<br>
<br>
IJ.log('Image filename is %s' % fname)<br>
<br>
# Iterate over all ROIs from ROI Manager<br>
rois = RoiManager.getInstance().getRoisAsArray()<br>
for i, roi in enumerate(rois):<br>
<br>
crop_id = i +1<br>
IJ.log("Opening crop %i / %i" % (crop_id, len(rois)))<br>
<br>
bounds = roi.getBounds()<br>
<br>
x = bounds.x<br>
y = bounds.y<br>
w = bounds.width<br>
h = bounds.height<br>
<br>
# Import only cropped region of the image<br>
options = ImporterOptions()<br>
options.setCrop(True)<br>
options.setCropRegion(0, Region(x, y, w, h))<br>
options.setId(fname)<br>
imps = BF.openImagePlus(options)<br>
<br>
imp = imps[0]<br>
imp.show()<br>
<br>
crop_basename = "crop%i_%s" % (crop_id, f.fileName)<br>
crop_fname = os.path.join(f.directory, crop_basename)<br>
imp.setTitle(crop_basename)<br>
<br>
# Save image<br>
IJ.log("Saving crop to %s" % crop_fname)<br>
saver = ImgSaver()<br>
saver.saveImg(crop_basename, imp)<br>
<br>
IJ.log('Done')<br>
<br>
It fails with this error:<br>
<br>
Traceback (most recent call last):<br>
File "/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py", line<br>
49, in <module><br>
saver.saveImg(crop_basename, imp)<br>
TypeError: saveImg(): 1st arg can't be coerced to io.scif.Writer, String<br>
<br>
at org.python.core.Py.TypeError(Py.java:235)<br>
at org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:209)<br>
at org.python.core.PyReflectedFunction.throwBadArgError(PyReflectedFunction.java:312)<br>
at org.python.core.PyReflectedFunction.throwError(PyReflectedFunction.java:321)<br>
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:167)<br>
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)<br>
at org.python.core.PyObject.__call__(PyObject.java:422)<br>
at org.python.core.PyObject.__call__(PyObject.java:426)<br>
at org.python.core.PyMethod.__call__(PyMethod.java:139)<br>
at org.python.pycode._pyx7.f$0(/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py:51)<br>
at org.python.pycode._pyx7.call_function(/home/hadim/local/Fiji.app/plugins/Crop_Multi_Roi.py)<br>
at org.python.core.PyTableCode.call(PyTableCode.java:165)<br>
at org.python.core.PyCode.call(PyCode.java:18)<br>
at org.python.core.Py.runCode(Py.java:1275)<br>
at org.scijava.plugins.scripting.jython.JythonScriptEngine.eval(JythonScriptEngine.java:76)<br>
at org.scijava.script.ScriptModule.run(ScriptModule.java:175)<br>
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167)<br>
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126)<br>
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65)<br>
at org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:164)<br>
at java.util.concurrent.FutureTask.run(FutureTask.java:262)<br>
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)<br>
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)<br>
at java.lang.Thread.run(Thread.java:745)<br>
<br>
Three questions:<br>
<br>
how can I write cropped image (I need to write them as OME Tiff so I<br>
need BF or SCIFIO).<br>
Is there is any “easy” alternative to BF setCropRegion function in SCIFIO ?<br>
I am currently using RoiManager.getInstance().getRoisAsArray() to<br>
retrieve all rois. How can I directly get a ROIManager instance from<br>
RoiSet.zip file ?<br>
<br>
Any help would be very appreciated.<br>
<br>
Thanks !<br>
<br>
—<br>
Hadrien Mary<br>
<br>
_______________________________________________<br>
ImageJ-devel mailing list<br>
<a href="mailto:ImageJ-devel@imagej.net">ImageJ-devel@imagej.net</a><br>
<a href="http://imagej.net/mailman/listinfo/imagej-devel" target="_blank">http://imagej.net/mailman/listinfo/imagej-devel</a><br>
</blockquote></div><br></div>