CellProfiler / python-bioformats

Read and write life sciences file formats
Other
131 stars 45 forks source link

Trouble converting .bif slides into .png #139

Closed rashindrie closed 1 year ago

rashindrie commented 3 years ago

Hi,

I am a novice user of the python-bioformats package and I am having trouble figuring out how to use it to read a .bif image and convert it into a .png using python.

I donwloaded the bioformats_package from http://downloads.openmicroscopy.org/bio-formats/5.0.1/artifacts/bioformats_package.jar and added it to the javabridge.

Can someone guide me on what I need to do next to convert the slide into a png?

I tried to obtain the pixel information using

omexmlstr = bioformats.get_omexml_metadata(path=slide_path)
o = bioformats.OMEXML(omexmlstr)
pixels = o.image().Pixels
print(pixels)

But encountered the following error in doing so:

Exception in thread "Thread-1" org.mozilla.javascript.WrappedException: Wrapped java.lang.NullPointerException (<java-python-bridge>#11)
    at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1754)
    at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:148)
    at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:225)
    at org.mozilla.javascript.optimizer.OptRuntime.call1(OptRuntime.java:32)
    at org.mozilla.javascript.gen._java_python_bridge__3._c_script_0(<java-python-bridge>:11)
    at org.mozilla.javascript.gen._java_python_bridge__3.call(<java-python-bridge>)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
    at org.mozilla.javascript.gen._java_python_bridge__3.call(<java-python-bridge>)
    at org.mozilla.javascript.gen._java_python_bridge__3.exec(<java-python-bridge>)
    at org.mozilla.javascript.Context.evaluateString(Context.java:1079)
Caused by: java.lang.NullPointerException
    at loci.formats.in.NDPISReader.initFile(NDPISReader.java:173)
    at loci.formats.FormatReader.setId(FormatReader.java:1315)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
    ... 9 more
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/javabridge/jutil.py", line 382, in run_script
    result = call(context, "evaluateString",
  File "/usr/local/lib/python3.8/site-packages/javabridge/jutil.py", line 892, in call
    result = fn(*nice_args)
  File "/usr/local/lib/python3.8/site-packages/javabridge/jutil.py", line 859, in fn
    raise JavaException(x)
javabridge.jutil.JavaException: Wrapped java.lang.NullPointerException (<java-python-bridge>#11)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/cdperera/PycharmProjects/ae_with_attention_based_classifier/temp.py", line 39, in <module>
    omexmlstr = bioformats.get_omexml_metadata(path=path)
  File "/usr/local/lib/python3.8/site-packages/bioformats/formatreader.py", line 1041, in get_omexml_metadata
    xml = jutil.run_script(script, dict(path=rdr.path, reader = rdr.rdr))
  File "/usr/local/lib/python3.8/site-packages/javabridge/jutil.py", line 399, in run_script
    raise JavaException(call(e.throwable, "unwrap", "()Ljava/lang/Object;"))
javabridge.jutil.JavaException: <Java object at 0x24784720>

So far the working code I have as follows:


import javabridge
import bioformats.formatreader

jars = javabridge.JARS + ["bioformats_package.jar"]
javabridge.start_vm(class_path=jars)
from pylab import *
import matplotlib.pyplot as plt

slide_path = './slide001.bif'

image_reader = bioformats.formatreader.make_image_reader_class()()
# # This sets up the readers to read the contents of the file if necessary
image_reader.allowOpenToCheckType(True)
image_reader.setId(slide_path)
n_c = image_reader.getSizeC()
n_t = image_reader.getSizeT()
pixel_type = image_reader.getPixelType()

# Make a fake ImageReader and install the one above inside it
wrapper = bioformats.formatreader.ImageReader(path=slide_path, perform_init=False)
wrapper.rdr = image_reader
wrapper.read()
print("This file has %d channels and %d time points" % (n_c, n_t))

This prints out a bunch of DEBUG lines and the following at the end:

.....
.....
00:36:41.935 [Thread-1] DEBUG loci.formats.tiff.TiffParser - Reading tile Length 200064 Offset 9803152
00:36:41.937 [Thread-1] DEBUG loci.formats.tiff.TiffParser - Reading tile Length 187560 Offset 10003216
This file has 3 channels and 11 time points

Versions