# myprint.py """Example of printing a dataset in your own format""" # Copyright (c) 2008-2012 Darcy Mason # This file is part of pydicom, relased under an MIT license. # See the file license.txt included with this distribution, also # available at http://pydicom.googlecode.com from __future__ import print_function def myprint(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) for data_element in dataset: if data_element.VR == "SQ": # a sequence print(indent_string, data_element.name) for sequence_item in data_element.value: myprint(sequence_item, indent + 1) print(next_indent_string + "---------") else: if data_element.name in dont_print: print("""""") else: repr_value = repr(data_element.value) if len(repr_value) > 50: repr_value = repr_value[:50] + "..." print("{0:s} {1:s} = {2:s}".format(indent_string, data_element.name, repr_value)) if __name__ == "__main__": import dicom import sys usage = """Usage: myprint filename""" if len(sys.argv) != 2: print(usage) sys.exit() ds = dicom.read_file(sys.argv[1]) myprint(ds)