|
a |
|
b/images/pngcovert.py |
|
|
1 |
import numpy as np |
|
|
2 |
import png |
|
|
3 |
import pydicom |
|
|
4 |
|
|
|
5 |
ds = pydicom.dcmread("/home/tkrsh/demo/images/0009.DCM") |
|
|
6 |
|
|
|
7 |
shape = ds.pixel_array.shape |
|
|
8 |
|
|
|
9 |
image_2d = ds.pixel_array.astype(float) |
|
|
10 |
|
|
|
11 |
image_2d_scaled = (np.maximum(image_2d,0) / image_2d.max()) * 255.0 |
|
|
12 |
|
|
|
13 |
image_2d_scaled = np.uint8(image_2d_scaled) |
|
|
14 |
|
|
|
15 |
destination = "/home/tkrsh/demo/images/dicom.jpeg" |
|
|
16 |
with open(destination, 'wb') as png_file: |
|
|
17 |
w = png.Writer(shape[1], shape[0], greyscale=True) |
|
|
18 |
w.write(png_file, image_2d_scaled) |