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

Mark Hiner hiner at wisc.edu
Fri Jan 9 15:14:31 CST 2015


Hi Hadrien,

I've identified at least two bugs that could be affecting you here.

First of all, there was a bug in the OME-TIFF format writing out
multi-dimensional images. That's fixed on a branch but not uploaded to Fiji
yet [1].

Second, it seems like cropping is restricting pixel reads to the correct
region but then doesn't change the image size.

I'm hoping when I fix the second issue writing cropped regions will be
fixed.

Thanks for the great feedback!
- Mark

[1] https://github.com/scifio/scifio-ome-xml/tree/fix-writing

On Fri, Jan 9, 2015 at 11:49 AM, Hadrien Mary <hadrien.mary at gmail.com>
wrote:

> I also confirm the same behaviour when replacing original file by OME
> sample data :
> http://www.openmicroscopy.org/Schemas/Samples/2013-06/bioformats-artificial/multi-channel-4D-series.ome.tif.zip
> .
>
> --
> 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 Fri, Jan 9, 2015 at 6:46 PM, Hadrien Mary <hadrien.mary at gmail.com>
> wrote:
> > After some investigations I think there is a bug with ImgSaver. In the
> > following script, X, Y crop is fine but axes informations (T and Z)
> > are lost and all frames appears on the same axes. Now if I replace
> > "target = "/home/hadim/cropped.tif" by "target =
> > "/home/hadim/cropped.ome.tif" on line 9. X, Y crop fails, cropped
> > image has the same dimensions than original however axes informations
> > (T and Z) are ok. Note that in cropped.ome.tif, pixel values are
> > totally incoherent which is not the case in cropped.tif.
> >
> > Files can be found here :
> >
> https://arwen.hadim.fr/public.php?service=files&t=71aa17336dcccb3437d9a3dff789d2c0
> >
> > Archive contains cropped.tif, cropped.ome.tif and original.ome.tif.
> >
> > The script:
> >
> > ---------------------------------------------------
> > from io.scif import SCIFIO
> > from io.scif.config import SCIFIOConfig
> > from io.scif.img import ImageRegion
> > from io.scif.img import ImgOpener
> > from io.scif.img import ImgSaver
> > from net.imglib2.meta import Axes
> >
> > fname = "/home/hadim/original.ome.tif"
> > target = "/home/hadim/cropped.tif"
> >
> > axes = [Axes.X, Axes.Y]
> > ranges = ["%i-%i" % (0, 15), "%i-%i" % (0, 25)]
> > config = SCIFIOConfig()
> > config.imgOpenerSetRegion(ImageRegion(axes, ranges))
> >
> > opener = ImgOpener()
> > imps = opener.openImgs(fname, config)
> > imp = imps[0]
> >
> > saver = ImgSaver()
> > saver.saveImg(target, imp)
> >
> > print('Done')
> > ---------------------------------------------------
> >
> >
> > --
> > Hadrien Mary
> >
> >
> > On Fri, Jan 9, 2015 at 12:25 AM, Hadrien Mary <hadrien.mary at gmail.com>
> wrote:
> >> Thanks for the tip (reminder to me: always look for example in tests!).
> >>
> >> Unfortunately crop does not work... I will try to do more tests
> >> tomorrow or wait for you or someone else to have a look.
> >>
> >> Updated code:
> >>
> >> from ij import IJ
> >> from ij import ImagePlus
> >>
> >> from io.scif import SCIFIO
> >> from io.scif.config import SCIFIOConfig
> >> from io.scif.img import ImageRegion
> >> from io.scif.img import ImgOpener
> >> from io.scif.img import ImgSaver
> >> from net.imglib2.meta import Axes
> >>
> >> fname = "/home/hadim/original.ome.tif"
> >> target = "/home/hadim/cropped.ome.tif"
> >>
> >> axes = [Axes.X, Axes.Y]
> >> ranges = ["%i-%i" % (2, 15), "%i-%i" % (2, 25)]
> >> config = SCIFIOConfig()
> >> config.imgOpenerSetRegion(ImageRegion(axes, ranges))
> >>
> >> opener = ImgOpener()
> >> imps = opener.openImgs(fname, config)
> >> imp = imps[0]
> >>
> >> saver = ImgSaver()
> >> saver.saveImg(target, imp)
> >>
> >> print('Done')
> >>
> >> --
> >> Hadrien Mary
> >>
> >>
> >> On Fri, Jan 9, 2015 at 12:09 AM, Curtis Rueden <ctrueden at wisc.edu>
> wrote:
> >>> 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
> >>>> >>
> >>>> >>
> >>>
> >>>
>
> _______________________________________________
> 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/20150109/b708b136/attachment-0001.html>


More information about the ImageJ-devel mailing list