Ter*_*ian 6 python dicom pydicom
I have to convert some files which come by default as .dcm to .png, I've found some code samples to achieve that around here but the end results are too bright. Could anybody have a look at this, please?
def convert_to_png(file):
ds = pydicom.dcmread(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
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0
# Convert to uint
image_2d_scaled = np.uint8(image_2d_scaled)
# Write the PNG file
with open(f'{file.strip(".dcm")}.png', 'wb') as png_file:
w = png.Writer(shape[1], shape[0], greyscale=True)
w.write(png_file, image_2d_scaled)
Run Code Online (Sandbox Code Playgroud)
I've tweaked around the code but nothing seems to work.
This is how the actual thing looks like as dicom and on the right side is the result of running this code

sca*_*ion 13
一些 DICOM 数据集需要原始像素强度的窗口中心/宽度重新缩放(通过VOI LUT 模块中的 (0028,1050)窗口中心和 (0028,1051)窗口宽度元素),以重现它们被“查看”的方式.
pydicom有一个函数apply_voi_lut()来应用这个窗口:
from pydicom import dcmread
from pydicom.pixel_data_handlers.util import apply_voi_lut
ds = dcmread(file)
if 'WindowWidth' in ds:
print('Dataset has windowing')
windowed = apply_voi_lut(ds.pixel_array, ds)
# Add code for rescaling to 8-bit...
Run Code Online (Sandbox Code Playgroud)
根据数据集类型,您可能需要事先使用apply_modality_lut()。