Sebastian
Sebastian

Reputation: 73

How to replace pixel data in dicom image with pydicom?

I'm trying to replace pixel data in given example image. My code:

import matplotlib.pyplot as plt
from pydicom import dcmread
from pydicom.data import get_testdata_file

fpath = get_testdata_file('CT_small.dcm')
ds = dcmread(fpath)

# replacing rows and columens with new values 
ds.Rows = 200 
ds.Columns = 200 
# replacing pixel data with new image (type: numpy.ndarray)
ds.pixel_array = data[layer].tobytes()
...

I get this error:

AttributeError: can't set attribute

UPDATE

Ok, if I use

ds.PixelData = data[layer].tobytes()

instead of

ds.pixel_array = data[layer].tobytes()

I get an image, but it looks totally different from the original image (data[layer]). What am I missing?

Upvotes: 2

Views: 2538

Answers (1)

Sebastian
Sebastian

Reputation: 73

This fixes my problem:

ds.PixelData = data[layer].astype(np.float16).tobytes()

Upvotes: 2

Related Questions