Hi guys,
i tried to write an OME-TIFF using python-bioformats but I got stuck. Any help is really appreciated
This is the script:
import bioformats.omexml as ome
import javabridge as jv
import bioformats
import os
import sys
import numpy as np
import bftools as bf
# Write file to disk
def writeOMETIFF(img_XYCZT, path, type='uint16'):
if verbose:
print('Dimensions XYCZT: ' + str(np.shape(img_XYCZT)))
sys.stdout.flush()
# Get the new dimensions
SizeX = np.shape(img_XYCZT)[0]
SizeY = np.shape(img_XYCZT)[1]
SizeC = np.shape(img_XYCZT)[2]
SizeZ = np.shape(img_XYCZT)[3]
SizeT = np.shape(img_XYCZT)[4]
# Start JVM for bioformats
bfpackage = r'c:\Users\m1srh\Documents\Software\Bioformats\5.9.2\bioformats_package.jar'
jars = jv.JARS + [bfpackage]
jv.start_vm(class_path=jars, run_headless=True, max_heap_size='4G')
# Getting metadata info
omexml = ome.OMEXML()
omexml.image(0).Name = os.path.split(path)[1]
p = omexml.image(0).Pixels
assert isinstance(p, ome.OMEXML.Pixels)
p.SizeX = SizeX
p.SizeY = SizeY
p.SizeC = SizeC
p.SizeT = SizeT
p.SizeZ = SizeZ
p.DimensionOrder = ome.DO_XYCZT
p.PixelType = type
p.channel_count = SizeC
p.plane_count = SizeZ
p.Channel(0).SamplesPerPixel = SizeC
omexml.structured_annotations.add_original_metadata(ome.OM_SAMPLES_PER_PIXEL, str(SizeC))
# Converting to omexml
xml = omexml.to_xml()
# Write file using Bioformats
for frame in range(SizeT):
index = frame
pixel_buffer = bioformats.formatwriter.convert_pixels_to_buffer(img_XYCZT[:, :, :, :, frame], type)
script = """
importClass(Packages.loci.formats.services.OMEXMLService,
Packages.loci.common.services.ServiceFactory,
Packages.loci.formats.out.TiffWriter);
var service = new ServiceFactory().getInstance(OMEXMLService);
var metadata = service.createOMEXMLMetadata(xml);
var writer = new TiffWriter();
writer.setBigTiff(true);
writer.setMetadataRetrieve(metadata);
writer.setId(path);
writer.setInterleaved(true);
writer.saveBytes(index, buffer);
writer.close();
"""
jv.run_script(script, dict(path=path, xml=xml, index=index, buffer=pixel_buffer))
##################################################################################################################
# Dimension TZCXY
SizeT = 30
SizeZ = 23
SizeC = 2
SizeX = 217
SizeY = 94
output_file = r'stackome.tiff'
img5d = np.random.randn(SizeX, SizeY, SizeC, SizeZ, SizeT).astype(np.uint16)
writeOMETIFF(img5d, output_file, type='uint16', verbose=True)
and the rror message is this:
C:\ProgramData\Anaconda3\python.exe "C:/scripts/ometiff_write3.py"
Dimensions XYCZT: (217, 94, 2, 23, 30)
Writing frames:
[1]
20:47:38.535 [Thread-0] DEBUG loci.common.services.ServiceFactory - Loaded properties from: services.properties
20:47:38.541 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.POIService and implementation class loci.formats.services.POIServiceImpl
20:47:38.542 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.MDBService and implementation class loci.formats.services.MDBServiceImpl
20:47:38.543 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.JPEGTurboService and implementation class loci.formats.services.JPEGTurboServiceImpl
20:47:38.544 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface ome.codecs.services.LuraWaveService and implementation class ome.codecs.services.LuraWaveServiceImpl
20:47:38.545 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.JAIIIOService and implementation class loci.formats.services.JAIIIOServiceImpl
20:47:38.547 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.WlzService and implementation class loci.formats.services.WlzServiceImpl
20:47:38.548 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.JHDFService and implementation class loci.formats.services.JHDFServiceImpl
20:47:38.549 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.NetCDFService and implementation class loci.formats.services.NetCDFServiceImpl
20:47:38.550 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.EXIFService and implementation class loci.formats.services.EXIFServiceImpl
20:47:38.551 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.MetakitService and implementation class loci.formats.services.MetakitServiceImpl
20:47:38.552 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.LuraWaveService and implementation class loci.formats.services.LuraWaveServiceImpl
20:47:38.553 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.OMEXMLService and implementation class loci.formats.services.OMEXMLServiceImpl
20:47:38.554 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface ome.codecs.services.JAIIIOService and implementation class ome.codecs.services.JAIIIOServiceImpl
20:47:38.554 [Thread-0] DEBUG loci.common.services.ServiceFactory - Added interface interface loci.formats.services.JPEGXRService and implementation class loci.formats.services.JPEGXRServiceImpl
20:47:38.865 [Thread-0] DEBUG ome.xml.model.primitives.Timestamp - Invalid timestamp '%(DEFAULT_NOW)s'
20:47:39.138 [Thread-0] DEBUG loci.common.NIOByteBufferProvider - Using mapped byte buffer? false
org.mozilla.javascript.WrappedException: Wrapped java.lang.IllegalArgumentException (<java-python-bridge>#12)
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.call2(OptRuntime.java:42)
at org.mozilla.javascript.gen._java_python_bridge__1._c_script_0(<java-python-bridge>:12)
at org.mozilla.javascript.gen._java_python_bridge__1.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__1.call(<java-python-bridge>)
at org.mozilla.javascript.gen._java_python_bridge__1.exec(<java-python-bridge>)
at org.mozilla.javascript.Context.evaluateString(Context.java:1079)
Caused by: java.lang.IllegalArgumentException
at java.nio.Buffer.position(Unknown Source)
at loci.common.NIOFileHandle.buffer(NIOFileHandle.java:632)
at loci.common.NIOFileHandle.readLong(NIOFileHandle.java:423)
at loci.common.RandomAccessInputStream.readLong(RandomAccessInputStream.java:640)
at loci.formats.tiff.TiffParser.getNextOffset(TiffParser.java:1301)
at loci.formats.tiff.TiffParser.getIFDOffsets(TiffParser.java:349)
at loci.formats.out.TiffWriter.saveBytes(TiffWriter.java:448)
at loci.formats.FormatWriter.saveBytes(FormatWriter.java:123)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:126)
... 9 more
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site-packages\javabridge\jutil.py", line 384, in run_script
scope, script, "<java-python-bridge>", 0, None)
File "C:\ProgramData\Anaconda3\lib\site-packages\javabridge\jutil.py", line 887, in call
result = fn(*nice_args)
File "C:\ProgramData\Anaconda3\lib\site-packages\javabridge\jutil.py", line 854, in fn
raise JavaException(x)
javabridge.jutil.JavaException: Wrapped java.lang.IllegalArgumentException (<java-python-bridge>#12)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/M1SRH/OneDrive - Carl Zeiss AG/Python_Projects/BioFormatsRead/ometiff_write3.py", line 146, in <module>
writeOMETIFF(img5d, output_file, type='uint16', verbose=True)
File "C:/Users/M1SRH/OneDrive - Carl Zeiss AG/Python_Projects/BioFormatsRead/ometiff_write3.py", line 79, in writeOMETIFF
jv.run_script(script, dict(path=path, xml=xml, index=index, buffer=pixel_buffer))
File "C:\ProgramData\Anaconda3\lib\site-packages\javabridge\jutil.py", line 394, in run_script
raise JavaException(call(e.throwable, "unwrap", "()Ljava/lang/Object;"))
javabridge.jutil.JavaException: <Java object at 0x13386790>