Skip to content

Instantly share code, notes, and snippets.

@paladin3895
Created January 21, 2021 13:05
Show Gist options
  • Save paladin3895/519e0ee1b17f3317f1e15f27a26bc264 to your computer and use it in GitHub Desktop.
Save paladin3895/519e0ee1b17f3317f1e15f27a26bc264 to your computer and use it in GitHub Desktop.
import cv2
import os
import pydicom
inputdir = './train/'
outdir = './png/'
#os.mkdir(outdir)
test_list = [ f for f in os.listdir(inputdir)]
for f in test_list[:10]: # remove "[:10]" to convert all images
ds = pydicom.read_file(inputdir + f) # read dicom image
img = ds.pixel_array # get image array
cv2.imwrite(outdir + f.replace('.dicom','.png'),img) # write png image
@hnguyentt
Copy link

hnguyentt commented Jan 21, 2021

import cv2
import os
import pydicom

inputdir = './train/'
outdir = './png/'
#os.mkdir(outdir)

test_list = [ f for f in  os.listdir(inputdir)]

for f in test_list[:10]:   # remove "[:10]" to convert all images
    ds = pydicom.read_file(inputdir + f) # read dicom image
    img = ds.pixel_array # get image array
    img = img.astype("float32")
    img = (img - img.max())/(img.max() - img.min())*255.
    cv2.imwrite(outdir + f.replace('.dicom','.png'),img) # write png image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment