Closed NicolasRannou closed 1 year ago
Currently it only supports reading, but there's no reason it can't also support writing NIFTI data at least in a Node.js environment. I'll keep this issue open as a feature request.
Yes thanks - Ideally it could generate the "right" bytestream and we could just save it out without nodejs!
I am also very interested in creating a web-based segmentation application and am looking for resources. Any news on this matter?
Currently it only supports reading, but there's no reason it can't also support writing NIFTI data at least in a Node.js environment. I'll keep this issue open as a feature request.
Any updates on this?
Yes thanks - Ideally it could generate the "right" bytestream and we could just save it out without nodejs!
any example to generate the right bytestream (serialize) the nifti header+data?
We are creating an online segmentation tool (MedSeg) and have a working solution for saving nifti masks, below is our code. It does save working nifti files where the header is copied from the user's loaded nifti file.
I would love it to save it as gzip though (.nii.gz), not sure how to do that. Anyone who has an idea on how it could be done? adding data = nifti.compress(data) or variants with compressing the blob does not work...
Code to get it to save a .nii:
function save_nifti(ctx){ var saveData = (function () { var a = document.createElement("a"); document.body.appendChild(a); a.style = "display: none"; return function (data, fileName) { var blob = new Blob(data); var url = window.URL.createObjectURL(blob); a.href = url; a.download = fileName; a.click(); window.URL.revokeObjectURL(url); }; }()); var data = [new Uint8Array(image_nii_header,0,image_nii_header.length), new Uint8Array(ctx.maskImage,0,ctx.maskImage.length)]; fileName = "mask"+thectx.fileName+".nii"; saveData(data, fileName); }
For gzipping data in js, you might want to take a look at https://github.com/nodeca/pako
Thanks, but that's exactly what nifti.compress(data) does (uses pako):
nifti.compress = function (data) { return pako.deflate(data); };
This function works, but even though it compresses, something happens that I don't understand with the nifti and it becomes a non-usable file even though it is small (expected size result, not too small either).
Finally figured it out!
Had to add a gzip wrapper, so in nifti-reader.js I changed to:
pako.deflate(data, {gzip: true})
Maybe this helps someone :)
@DLinRadiology as you note, this library supports writing NIfTIs. For a complex but comprehensive example, see the NiiVue drawing demo that includes a Save Drawing
item that writes a NIfTI file.
Is there a way to export some data as NIFTI using this library?
A use case it to generate a label-map/segmentation in JavaScript then being able to save it as a NIFTI file.
Thanks