-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew converter
33 lines (24 loc) · 1.18 KB
/
new converter
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 numpy as np
import png, os, pydicom
source_folder = r'C:\Users\Omar-ElQady\OneDrive - Faculty of Computers and Information\Desktop\dcm\\'
output_folder = r'C:\Users\Omar-ElQady\OneDrive - Faculty of Computers and Information\Desktop\jpg\\'
def dicom2png(source_folder, output_folder):
list_of_files = os.listdir(source_folder)
for file in list_of_files:
try:
ds = pydicom.dcmread(os.path.join(source_folder,file))
shape = ds.pixel_array.shape
# Convert to float to avoid overflow or underflow losses.
image_2d = ds.pixel_array.astype(float)
# Rescaling grey scale between 0-255
threshold = 500
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()+ threshold) * 255.0
# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)
# Write the PNG file
with open(os.path.join(output_folder,file)+'.png' , 'wb') as png_file:
w = png.Writer(shape[1], shape[0], greyscale=True)
w.write(png_file, image_2d_scaled)
except:
print('Could not convert: ', file)
dicom2png(source_folder, output_folder)