Last active
October 11, 2018 09:41
-
-
Save fladd/3b99a6bbe13d25be8b5c812b2f656f3f to your computer and use it in GitHub Desktop.
Rename DICOM files to Turbo-BrainVoyager format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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