【发布时间】:2020-02-14 03:34:17
【问题描述】:
我必须将一些默认为.dcm 的文件转换为.png,我在这里找到了一些代码示例来实现这一点,但最终结果太亮了。请问有人可以看一下吗?
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)
我已经对代码进行了调整,但似乎没有任何效果。
【问题讨论】: