[英]I have a Data in DICOM images i want to convert dicom images in to png or jpg format . but how to convert Number of folders at once with for loop
I have Data in DICOM images, and I want to convert dicom images into png or jpg format.我有 DICOM 图像中的数据,我想将 dicom 图像转换为 png 或 jpg 格式。 However, I need to convert the images in multiple folders at once using a for-loop.
但是,我需要使用 for 循环一次转换多个文件夹中的图像。 Here what I've done so far:
这是我到目前为止所做的:
import pydicom as dicom
import cv2
# specify your image path
image_path = 'Data/LungSegmentation/NSCLC-Radiomics/LUNG1-001/09-18-2008-StudyID-69331/0.000000-82046/1-001.dcm'
ds = dicom.dcmread(image_path)
pixel_array_numpy = ds.pixel_array
image_format = '.jpg' # or '.png'
image_path = image_path.replace('.dcm', image_format)
cv2.imwrite(image_path, pixel_array_numpy)
#show image.....
plt.imshow(ds.pixel_array,cmap ='gray',vmin=0,vmax=255)
Iterate over files and folders with python modules like glob
.使用
glob
等 python 模块迭代文件和文件夹。 Just a little example:只是一个小例子:
import glob
folders = glob.glob("PATH_CONTAINING_YOUR_FOLDERS")
for folder in folders:
files = glob.glob(folder+'\*.dcm') # pick only dicom files
for file in files:
DO_YOUR_STUFF_HERE(file)
I've build a package to convert dicom files to jpg/png/bmp, you just pip install dicom2jpg
then use我已经构建了一个package来将 dicom 文件转换为 jpg/png/bmp,你只需
pip install dicom2jpg
然后使用
import dicom2jpg
dicom_dir = "/Users/user/Desktop/dir_a"
export_location = "/Users/user/Desktop/dir_b"
# convert all DICOM files in dicom_dir folder to jpg format
dicom2jpg.dicom2jpg(dicom_dir, export_location)
# convert all DICOM files in dicom_dir folder to png format
dicom2jpg.dicom2png(dicom_dir, export_location)
hope it would help希望它会有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.