From d4f52c19472f487a4721445d7b7079d0ba4cf361 Mon Sep 17 00:00:00 2001 From: Ross Date: Thu, 25 Feb 2021 21:54:12 +0000 Subject: [PATCH] . --- helpers/images.py | 41 ++++++++++++++++++++++++++++++++++++++++- longs/models.py | 35 +++++++++++++++++++++++++++++++++++ longs/urls.py | 1 + longs/views.py | 4 ++++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/helpers/images.py b/helpers/images.py index c229dd14..8057ae03 100644 --- a/helpers/images.py +++ b/helpers/images.py @@ -6,6 +6,8 @@ from io import BytesIO from PIL import Image, ImageFile import pydicom +import sys +import glob try: from .pydicom_PIL import get_PIL_image @@ -71,4 +73,41 @@ def pil_dicom_image(source, exif_orientation=True, **options): def show_PIL(dataset): """Display an image using the Python Imaging Library (PIL)""" im = get_PIL_image(dataset) - im.show() \ No newline at end of file + im.show() + + + + +def get_dicom_order(path): + # load the DICOM files + files = [] + + for fname in glob.glob(path, recursive=False): + print("loading: {}".format(fname)) + files.append((fname, pydicom.dcmread(fname))) + + print("file count: {}".format(len(files))) + + # skip files with no SliceLocation (eg scout views) + slices = [] + map = {} + skipcount = 0 + for fname, f in files: + print(dir(f)) + print(fname) + if hasattr(f, 'SliceLocation'): + slices.append(f) + map[f.SliceLocation] = fname + else: + skipcount = skipcount + 1 + + print("skipped, no SliceLocation: {}".format(skipcount)) + + # ensure they are in the correct order + slices = sorted(slices, key=lambda s: s.SliceLocation) + + #print(slices) + for i in slices: + print(map[i.SliceLocation]) + +get_dicom_order("/home/ross/Downloads/DICOM HEAD/STD4/SER1/*.dcm") \ No newline at end of file diff --git a/longs/models.py b/longs/models.py index c6796a07..c5558cc0 100644 --- a/longs/models.py +++ b/longs/models.py @@ -32,6 +32,8 @@ from generic.models import Examination, Condition, Sign, ExamBase, Plane, Contra from easy_thumbnails.files import get_thumbnailer from easy_thumbnails.exceptions import InvalidImageFormatError +import pydicom + image_storage = FileSystemStorage( @@ -283,6 +285,39 @@ class LongSeries(models.Model): "
{}
{}
Images: {}
", examination, thumb, image_number ) + def order_by_dicom_location(self): + images = self.images.all() + + files = [] + + for i in images: + files.append((i, pydicom.dcmread(i.image.path))) + + #print("file count: {}".format(len(files))) + + # skip files with no SliceLocation (eg scout views) + slices = [] + map = {} + skipcount = 0 + for i, f in files: + if hasattr(f, 'SliceLocation'): + slices.append(f) + map[f.SliceLocation] = i + else: + skipcount = skipcount + 1 + + print("skipped, no SliceLocation: {}".format(skipcount)) + + # ensure they are in the correct order + slices = sorted(slices, key=lambda s: s.SliceLocation) + + #print(slices) + n = 1 + for f in slices: + i = map[f.SliceLocation] + i.position = n + i.save() + n = n + 1 # class LongImage(models.Model): # long = models.ForeignKey(Long, diff --git a/longs/urls.py b/longs/urls.py index d45ba515..7252f3f6 100755 --- a/longs/urls.py +++ b/longs/urls.py @@ -11,6 +11,7 @@ urlpatterns = [ path("question/", views.LongView.as_view(), name="long_view"), path("series/", views.LongSeriesView.as_view(), name="long_series_view"), path("series/", views.long_series_detail, name="long_series_detail"), + path("series//order_dicom", views.long_series_order_dicom, name="long_series_order_dicom"), path("series//delete", views.LongSeriesDelete.as_view(), name="long_series_delete"), # path("unchecked/", views.unchecked_list, name="unchecked_list"), # path("verified//", views.verified_detail, name="verified_detail"), diff --git a/longs/views.py b/longs/views.py index d2c32383..9419125e 100755 --- a/longs/views.py +++ b/longs/views.py @@ -930,5 +930,9 @@ def exam_scores_cid_user(request, pk, sk): }, ) +@login_required +def long_series_order_dicom(request, pk): + series = get_object_or_404(LongSeries, pk=pk) + series.order_by_dicom_location() LongExamViews = ExamViews(Exam, Long, "longs", "long", loadJsonAnswer) \ No newline at end of file