Skip to content

Instantly share code, notes, and snippets.

@fladd
Last active October 11, 2018 09:41
Show Gist options
  • Save fladd/3b99a6bbe13d25be8b5c812b2f656f3f to your computer and use it in GitHub Desktop.
Save fladd/3b99a6bbe13d25be8b5c812b2f656f3f to your computer and use it in GitHub Desktop.
Rename DICOM files to Turbo-BrainVoyager format
"""Rename DICOM files to Turbo-BrainVoyager format.
Works with:
- BrainVoyager .dcm files
- Siemens .IMA files
Usage:
python rename_dicoms_tbv.py [<directory>]
If <directory> is not specified, a GUI dialogue will ask for it.
"""
__author__ = 'Florian Krause <[email protected]>'
__date__ = '2017-11-22'
import sys
import os
import glob
try:
directory = sys.argv[1]
assert os.path.exists(directory)
except:
if sys.version[0] == '2':
from tkFileDialog import askdirectory
elif sys.version[0] == '3':
from tkinter.filedialog import askdirectory
directory = askdirectory()
if directory == "":
sys.exit()
dcm_files = glob.glob(os.path.join(directory, "*.dcm"))
ima_files = glob.glob(os.path.join(directory, "*.IMA"))
for f in dcm_files:
path, filename = os.path.split(f)
parts = os.path.splitext(filename)[0].split("-")
new_filename = "001_{0}_{1}.dcm".format(str(int(parts[-3])).zfill(6),
str(int(parts[-1])).zfill(6))
os.rename(f, os.path.join(path, new_filename))
for f in ima_files:
path, filename = os.path.split(f)
parts = os.path.splitext(filename)[0].split(".")
new_filename = "001_{0}_{1}.dcm".format(str(int(parts[-10])).zfill(6),
str(int(parts[-9])).zfill(6))
os.rename(f, os.path.join(path, new_filename))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment