Created
March 9, 2024 15:30
-
-
Save ZFTurbo/3d48a014052b8b30a1c702096e2e975c to your computer and use it in GitHub Desktop.
DICOM to PNG Converter
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
# coding: utf-8 | |
__author__ = 'ZFTurbo: https://github.com/ZFTurbo/' | |
import os | |
import glob | |
import cv2 | |
import pydicom | |
import argparse | |
def convert_dataset(args): | |
inp_path = args.input_folder | |
out_path = args.output_folder | |
if not os.path.isdir(out_path): | |
os.mkdir(out_path) | |
files = glob.glob(inp_path + '*/**') | |
print('Files: {}'.format(len(files))) | |
for f in files: | |
print(f) | |
try: | |
ds = pydicom.dcmread(f) | |
except: | |
print('cant read file!') | |
continue | |
img = ds.pixel_array | |
print(img.shape, img.dtype) | |
cv2.imwrite(out_path + os.path.basename(f) + '.png', img) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--input_folder", type=str, default='./', help="Folder where dicom files are located") | |
parser.add_argument("--output_folder", type=str, default='./', help="Folder where to store PNG files") | |
args = parser.parse_args() | |
convert_dataset(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment