Skip to content

Instantly share code, notes, and snippets.

@dave3d
Created July 31, 2021 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dave3d/bf1f961a2628dd489ed09cbe9c0d6aae to your computer and use it in GitHub Desktop.
Save dave3d/bf1f961a2628dd489ed09cbe9c0d6aae to your computer and use it in GitHub Desktop.
A script that prints the series IDs (and optionally the entire metadata dictionary) of DICOM images.
#! /usr/bin/env python
import sys, getopt
# By default, this script prints out the seriesID of any dicom
# images. With the Verbose flag, it prints out the whole metadata
# dictionary.
seriesID = False
verbose = 0
def usage():
print ("")
print ("dicomdump.py: [options] dicom_image")
print ("")
print (" -h, --help This help message")
print (" -v, --verbose Increase verbosity level")
print (" -s, --series Print series ID")
try:
opts, args= getopt.getopt( sys.argv[1:], "vhs",
[ "verbose", "help", "series" ] )
except getopt.GetoptErr as err:
print (str(err))
usage()
sys.exit(1)
for o, a in opts:
if o in ("-v", "--verbose"):
verbose = verbose + 1
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-s", "--series"):
seriesID = True
else:
assert False, "unhandled options"
import SimpleITK as sitk
def dump_dicomstuff(fname):
print ("\nFile:", fname)
img = sitk.ReadImage(fname)
if verbose>1:
print (img)
keys = img.GetMetaDataKeys()
#if verbose:
# print (keys)
for k in keys:
v = img.GetMetaData(k)
if verbose:
print (k, ": ", v)
elif seriesID and (k in ("0020|000e")):
print (k, ": ", v)
for fname in args:
dump_dicomstuff(fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment