Let's Run Jinyeah

[Python] Modify DICOM image and save as DICOM 본문

Programming/Python

[Python] Modify DICOM image and save as DICOM

jinyeah 2022. 5. 19. 15:19

read dicom image

import pydicom as dcm

D_TYPE = 'float32'
ds = dcm.dcmread('filename.dcm')
image = ds.pixel_array.astype(D_TYPE)

save gray-scale image to dicom

import pydicom as dcm

D_TYPE = 'float32'
num_bits = 16

ds = dcm.dcmread('filename.dcm')
image = ds.pixel_array.astype(D_TYPE)

"""
image processing for gray image....
output = gray_image (dtype: float32, pixel_range = [0.0, 1.0])
"""

gray_image = (gray_image * (2 ** num_bits - 1)).astype('uint%d' % num_bits)
ds.PixelData = phase_map.tobytes()
ds.Rows, ds.Columns = phase_map.shape
ds.SamplesPerPixel = 1 # channel
ds.BitsAllocated = num_bits
ds.BitsStored = num_bits

ds.save_as(save_path.dcm)

 

save rgb image to dicom

import pydicom as dcm

ds = dcm.dcmread('filename.dcm')
num_bits = 8

"""
image processing for color image....
output = color_image
"""

color_image = (color_image * (2 ** num_bits - 1)).astype('uint%d' % num_bits)

ds.PixelData = color_image.tobytes()
ds.Rows, ds.Columns = color_image.shape[:2]
ds.SamplesPerPixel = 3 # channel
ds.PhotometricInterpretation = 'RGB'
ds.BitsAllocated = num_bits
ds.BitsStored = num_bits

ds.save_as(save_path.dcm)

 

'Programming > Python' 카테고리의 다른 글

Numpy 모음  (0) 2022.06.17
[Python] Read/Write csv file (csv, Pandas)  (0) 2022.05.25
[Python] 가상환경 생성 및 활성화  (0) 2022.05.17
[Python] List method  (0) 2021.10.19
[Python] List - Negative Slicing  (0) 2021.01.25
Comments