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

Curtis Rueden ctrueden at wisc.edu
Thu Jan 8 17:09:00 CST 2015


Hi Hadrien,

> I would like to be able to crop a region in X and Y while
> keeping any other dimensions which could exist (C, Z, T).

Here's some Java:

    int minX = 128, maxX = 255, minY = 128, minY = 255;
    AxisType[] axes = { Axes.X, Axes.Y };
    String[] ranges = { minX + "-" + maxX, minY + "-" + maxY };
    config.imgOpenerSetRegion(new ImageRegion(axes, ranges));

Adapted from here:
https://github.com/scifio/scifio/blob/scifio-0.18.0/src/test/java/io/scif/img/utests/ImgOpenerTest.java#L189-L191

And untested.

HTH,
Curtis

On Thu, Jan 8, 2015 at 4:47 PM, Hadrien Mary <hadrien.mary at gmail.com> wrote:

> After some investigations, I'm pretty sure I don't instanciate
> ImageRegion correctly. Doc and source code didn't help me... I would
> like to be able to crop a region in X and Y while keeping any other
> dimensions which could exist (C, Z, T).
>
> --
> Hadrien Mary
>
>
>
> On Thu, Jan 8, 2015 at 10:22 PM, Hadrien Mary <hadrien.mary at gmail.com>
> wrote:
> > Thank you Curtis for the answer.
> >
> > I tried to apply what you told me and the code now works without
> > error. However the saved cropped image is not cropped (same size as
> > original) and pixel values are modified).
> >
> > Script:
> >
> > from io.scif.config import SCIFIOConfig
> > from io.scif.img import ImageRegion
> > from io.scif.img import ImgOpener
> > from io.scif.img import ImgSaver
> >
> > fname = "/home/hadim/original.ome.tif"
> > target = "/home/hadim/cropped.ome.tif"
> >
> > config = SCIFIOConfig()
> > region = ImageRegion(dict(x=2, y=2, width=10, height=10))
> > config.imgOpenerSetRegion(region)
> >
> > opener = ImgOpener()
> > imps = opener.openImgs(fname, config)
> > imp = imps[0]
> >
> > print(imps)
> >
> > saver = ImgSaver()
> > saver.saveImg(target, imp)
> >
> > Thanks again for your time. Don't be sorry if you don't have time to
> > write an example.
> >
> > I will be happy to provide some python/scifio examples scripts.
> >
> >
> > --
> > Hadrien Mary
> >
> > Ph.D student in Biology
> > Tournier-Gachet Team
> > CNRS - LBCMCP - UMR 5088
> >
> > Université de Toulouse - Bât. 4R3B1
> > 118, route de Narbonne - 31062 Toulouse
> >
> >
> > On Thu, Jan 8, 2015 at 9:56 PM, Curtis Rueden <ctrueden at wisc.edu> wrote:
> >> 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/bd3e5595/attachment-0001.html>


More information about the ImageJ-devel mailing list