-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilitario.py
33 lines (23 loc) · 841 Bytes
/
utilitario.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import pydicom
import cv2
# Convert DICOM to JPG/PNG via openCV
def convert_images(filename, outdir, img_type='jpg'):
"""Reads a dcm file and saves the files as png/jpg
Args:
filename: path to the dcm file
img_type: format of the processed file (jpg or png)
https://discuss.pytorch.org/t/dicom-files-in-pytorch/49058/5
"""
# extract the name of the file
name = filename.parts[-1]
# read the dcm file
ds = pydicom.read_file(str(filename))
img = ds.pixel_array
# save the image as jpg/png
if img_type=="jpg":
cv2.imwrite(outdir + name.replace('.dcm' ,'.jpg'), img)
else:
cv2.imwrite(outdir + name.replace('.dcm' ,'.png'), img)
# Using dask
# all_images = [dd.delayed(convert_images)(all_files[x]) for x in range(len(all_files))]
# dd.compute(all_images)