在 Django 中使用 Nibabel
Using Nibabel in Django
我正在制作一个将 .nii 文件转换为 png(zip) 的网络应用程序。
我已经在 python 中实现了主要逻辑,但在将其移植到 Web 应用程序时遇到问题。
所以我想创建一个接受 .nii 文件并输出包含所有 .png 切片的 zip 文件的表单。到目前为止,我已经写了一个简单的视图:
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)
不出所料,它不起作用,因为 nibabel.load 函数需要路径,而不是对象 InMemoryUploadedFile。但我不知道还能做什么!
所以我使用不同的文件上传处理程序解决了这个问题,
FILE_UPLOAD_HANDLERS = ['django.core.files.uploadhandler.TemporaryFileUploadHandler']
它有一个函数 temporary_file_path()
,然后我将其传递给 nibabel.load()
函数,瞧!问题已解决。
我正在制作一个将 .nii 文件转换为 png(zip) 的网络应用程序。 我已经在 python 中实现了主要逻辑,但在将其移植到 Web 应用程序时遇到问题。
所以我想创建一个接受 .nii 文件并输出包含所有 .png 切片的 zip 文件的表单。到目前为止,我已经写了一个简单的视图:
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)
不出所料,它不起作用,因为 nibabel.load 函数需要路径,而不是对象 InMemoryUploadedFile。但我不知道还能做什么!
所以我使用不同的文件上传处理程序解决了这个问题,
FILE_UPLOAD_HANDLERS = ['django.core.files.uploadhandler.TemporaryFileUploadHandler']
它有一个函数 temporary_file_path()
,然后我将其传递给 nibabel.load()
函数,瞧!问题已解决。