Search code examples
pythondjangonifti

Using Nibabel in Django


I am making a web app that converts .nii files to png(zip). I have implemented the main logic in python but am facing problems porting it to the web app.

So I want to create a form that accepts a .nii file and outputs a zip file containing all the .png slices. So far I've written a simple view:

Views.py

from django.shortcuts import render
from .forms import SharingForms

from django.http import HttpResponse
import imageio,nibabel,numpy
from zipfile import ZipFile
from .models import NII


def index(request, **kwargs):
    if request.method == 'POST':
        form = SharingForms(request.POST,request.FILES)
        if form.is_valid():
            for field in request.FILES.keys():
                for formfile in request.FILES.getlist(field):
                    file = NII(file = formfile)
                    file.save()

                    response = HttpResponse(content_type='application/zip')
                    zip_file = ZipFile(response, 'w')
                    image_array = nibabel.load(file).get_fdata()

                    if len(image_array.shape) == 4:
                      # set 4d array dimension values
                        nx, ny, nz, nw = image_array.shape

                    total_volumes = image_array.shape[3]
                    total_slices = image_array.shape[2] 

                    for current_volume in range(0, total_volumes):
                        slice_counter = 0
                        # iterate through slices
                        for current_slice in range(0, total_slices):
                            if (slice_counter % 1) == 0:
                                # rotate or no rotate
                                data = image_array[:, :, current_slice, current_volume]

                            #alternate slices and save as png
                            print('Saving image...')
                            image_name = file[:-4] + "_t" + "{:0>3}".format(str(current_volume+1)) + "_z" + "{:0>3}".format(str(current_slice+1))+ ".png"
                            imageio.imwrite(image_name, data)
                            print('Saved.')

                            zip_file.write(image_name)

                    zip_file.close()
                    response['Content-Disposition'] = 'attachment; filename={}'.format(file)
                    return response

                   #response = HttpResponse(content_type='application/zip')
                    #zip_file = zipfile.ZipFile(response, 'w')
                    #for filename in filenames:
                    #  zip_file.write(filename)
                    #response['Content-Disposition'] = 'attachment; filename={}'.format(zipfile_name)
                    #return response


    else:
        form = SharingForms(request.POST,request.FILES)
    return render(request,'index.html',{'form':form})

Models.py

from django.db import models

class NII(models.Model):
    file = models.FileField(upload_to='upload_data')

    def __str__(self):
        return str(file)

Unsurprisingly it dosen't work as the nibabel.load function requires a path, and not an objector InMemoryUploadedFile. But I don't know what else to do!


Solution

  • So I resolved the issue by using a different File Upload Handler,

    FILE_UPLOAD_HANDLERS = ['django.core.files.uploadhandler.TemporaryFileUploadHandler']
    

    This has a function temporary_file_path() which I then passed on to the nibabel.load() function and voila! problem solved.

    Reference: https://docs.djangoproject.com/en/3.0/ref/files/uploads/