Search code examples
pythonopencvpydicom

Dicom to PNG Conversion - output image is completely black


While conversion of dicom to png using Python, without getting any error. Output file is complete Black, although image array have variable values.

Code:

import pydicom
import cv2
import os

dicom = pydicom.dcmread(inputdir + f)
# print("dicom", dicom)
img = dicom.pixel_array
print("img", img)
cv2.imwrite(outdir + f.replace('.dcm','.png'),img)

Image array (for reference):

[[585 585 570 ... 570 572 570]
[585 585 585 ... 572 575 572]
[585 585 585 ... 553 568 575]
...
[854 854 854 ... 778 783 787]
[854 854 856 ... 783 785 787]
[854 856 856 ... 785 790 759]]

Solution

  • Adding the next line to code after "dicom.pixel_array" will do the trick.

    ## Rescaling grey scale between 0-255
    scaled_img = (np.maximum(img,0) / img.max()) * 255.0