-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcm2png.py
39 lines (30 loc) · 1.17 KB
/
dcm2png.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
34
35
36
37
38
39
import os
import pydicom
from PIL import Image
from tqdm import tqdm
from multiprocessing.dummy import Pool
from concurrent.futures import ThreadPoolExecutor
import numpy as np
# 遍历文件夹中的所有文件,并添加进度条
def process_file(file):
# 如果文件是DCM格式,则进行转换
if file.endswith(".dcm"):
# 读取DCM文件
dcm = pydicom.dcmread(file)
# dcm -> 16bit PNG
pixel_array = dcm.pixel_array
pixel_array = (pixel_array - np.min(pixel_array)) / (np.max(pixel_array) - np.min(pixel_array))
pixel_array = (pixel_array * 65535).astype(np.uint16)
# Save
image = Image.fromarray(pixel_array)
png_path = file.replace(".dcm", ".png")
image.save(png_path)
if __name__ == "__main__":
# 指定要遍历的文件夹路径
folder_path = r"I:\dataset\Med\2023_Med_CQK"
with ThreadPoolExecutor(max_workers=20) as executor:
for root, dirs, files in tqdm(os.walk(folder_path)):
for file in files:
if file.endswith(".dcm"):
executor.submit(process_file, os.path.join(root, file))
print("Conversion complete.")