diff --git a/helpers/images.py b/helpers/images.py index 6232640a..57c2a718 100644 --- a/helpers/images.py +++ b/helpers/images.py @@ -110,4 +110,38 @@ def get_dicom_order(path): 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 +#get_dicom_order("/home/ross/Downloads/DICOM HEAD/STD4/SER1/*.dcm") + +def pretty_print_dicom(dataset, indent=0): + return "\n
".join(print_dicom(dataset, indent)) + + +def print_dicom(dataset, indent=0): + """Go through all items in the dataset and print them with custom format + + Modelled after Dataset._pretty_str() + """ + dont_print = ['Pixel Data', 'File Meta Information Version'] + + indent_string = " " * indent + next_indent_string = " " * (indent + 1) + + l = [] + + for data_element in dataset: + if data_element.VR == "SQ": # a sequence + l.append(f"({indent_string}{data_element.name}") + for sequence_item in data_element.value: + l.append(print_dicom(sequence_item, indent + 1)) + l.append(next_indent_string + "---------") + else: + if data_element.name in dont_print: + l.append("""""") + else: + repr_value = repr(data_element.value) + if len(repr_value) > 50: + repr_value = repr_value[:50] + "..." + l.append("{0:s} {1:s} = {2:s}".format(indent_string, + data_element.name, + repr_value)) + return l \ No newline at end of file diff --git a/longs/models.py b/longs/models.py index 863c9f2a..fc590b6b 100644 --- a/longs/models.py +++ b/longs/models.py @@ -24,7 +24,7 @@ from sortedm2m.fields import SortedManyToManyField import string from collections import defaultdict -from helpers.images import image_as_base64 +from helpers.images import image_as_base64, pretty_print_dicom from anatomy.models import Modality @@ -317,7 +317,7 @@ class LongSeriesImage(models.Model): def get_dicom_info(self): try: - info = pydicom.read_file(self.image).to_json() + info = pretty_print_dicom(pydicom.read_file(self.image)) except pydicom.errors.InvalidDicomError: info = "File is not a dicom." return(info)