Last active
June 22, 2022 09:18
-
-
Save felixbd/981d9b0b17cf7ab18cd8bf1710dda3e1 to your computer and use it in GitHub Desktop.
convert dicom images
This file contains hidden or 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
#!/usr/bin/env python3.9 | |
# -*- coding: utf-8 -*- | |
""" | |
Do What the Fuck You Want To Public License - Use at your own risk, no warranty! | |
""" | |
import os | |
import sys | |
import argparse | |
import traceback | |
import matplotlib.pyplot as plt | |
import pydicom | |
import pydicom.data | |
def main() -> None: | |
parser = argparse.ArgumentParser(description="convert a dicom img to another img format") | |
parser.add_argument('-f', '--file', type=str, nargs=1, default=None) | |
parser.add_argument('-o', '--out', type=str, nargs=1, default=None) | |
parser.add_argument('-r'. '--root', type=str, nargs=1, default=None) | |
args = parser.parse_args() | |
if not args.file or not args.out: | |
raise SystemError('MISSING ARGUMENT ERROR') | |
dicom_file_name: str = args.file[0] | |
img_out_file_name: str = args.out[0] | |
try: | |
base = args.root[0] # path of project root | |
filename = pydicom.data.data_manager.get_files(base, dicom_file_name)[0] | |
ds = pydicom.dcmread(filename) | |
my_img = plt.imshow(ds.pixel_array, cmap=plt.cm.bone) | |
# plt.show() | |
plt.imsave(img_out_file_name, my_img[1]) | |
except Exception as err: | |
traceback.print_exc(file=sys.stdout) | |
sys.exit(f'\033[1;31m \n\nError: {err.__class__.__name__}\nMessage: {str(err)}') | |
if __name__ == "__main__": | |
main() |
This file contains hidden or 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
matplotlib | |
pydicom |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment