I was trying to write a 4d c-z-x-y series from python today, and tried both pyimagej and python-bioformats. I wasn’t able to get either to work perfectly.
First I create a nd numpy array
# 4 channels, 50 slices, 512 by 512 nc=4 nz = 50 ny = 512 nx = 512 test=((2**16-1)*np.random.rand(nc, nz, ny, nx)).astype('uint16')
Then try to save with pyimagej
import imagej ij=imagej.init() dataset=ij.py.to_dataset(test) ij.io().save(dataset, 'frompyimagej.tif')
It saves, but it doesn’t seem to save the axis info. When loading into Fiji I get a 3D array with 200 frames instead of a 4d array. Any advice on how to save as 4d? I also tried converting to xarray first but that didn’t work.
I also tried python-bioformats
import bioformats as bf bf.write_image("test.tif",test,bf.PT_UINT16)
That didn’t work, as it failed with an error, however I’m not sure I expected it to, because if I remember I have to set things up and write one plane at a time with python-bioformats. Is that correct?
I actually don’t have a preference at all between bioformats and pyimagej. Whatever one works, and will be supported in the future is fine with me.