pixel_array: cannot reshape array of size 51200 into shape (320,320)
zeakey opened this issue · comments
Describe the issue
I'm trying to read the pixel data of a dicom with ds.pixel_array
but got the error message in the title, which indicates the data in ds.PixelData
does not match ds.Rows
and ds.Columns
.
I guess either the rows/columns or the PixelData was incorrectly assigned by some software, leading to the error.
However, I can visualize the image in Osirix and Slicer3d.
The code to reproduce this error is pretty simple, but I couldn't share the dicom file here.
from pydicom import dcmread
ds = dcmread("IM-0003-0001.dcm")
ds.pixel_array # error occurs here
- The output from:
For pydicom < 2.3:
from pydicom import dcmread
ds = dcmread("/path/to/the/dataset")
print(ds.file_meta.get("TransferSyntaxUID", "(no transfer syntax)"))
print(ds.group_dataset(0x0028))
print(ds.group_dataset(0x7FE0))
In [4]: ds = dcmread("IM-0003-0001.dcm")
In [5]: ds.file_meta.get("TransferSyntaxUID", "(no transfer syntax)")
Out[5]: '1.2.840.10008.1.2.4.90'
In [6]: ds.group_dataset(0x0028)
Out[6]:
(0028, 0002) Samples per Pixel US: 1
(0028, 0004) Photometric Interpretation CS: 'MONOCHROME2'
(0028, 0010) Rows US: 320
(0028, 0011) Columns US: 320
(0028, 0030) Pixel Spacing DS: [0.625, 0.625]
(0028, 0100) Bits Allocated US: 16
(0028, 0101) Bits Stored US: 12
(0028, 0102) High Bit US: 11
(0028, 0103) Pixel Representation US: 0
(0028, 0106) Smallest Image Pixel Value US: 0
(0028, 0107) Largest Image Pixel Value US: 320
(0028, 1050) Window Center DS: '145.0'
(0028, 1051) Window Width DS: '494.0'
(0028, 1055) Window Center & Width Explanation LO: 'WINDOW1'
In [7]: ds.group_dataset(0x7FE0)
Out[7]: (7fe0, 0010) Pixel Data OB: Array of 71272 elements
Do you know which plugin is doing the decompression?
Related, please also post the contents of running
python -m pydicom.env_info
from a command line.
Can you post the results from below as well?
print(" ".join(f"{b:02X}" for b in ds.PixelData[:100]))
@zeakey did you resolve your issue?
@scaramallion Thanks for your response and the results are below:
print(" ".join(f"{b:02X}" for b in ds.PixelData[:100]))
FE FF 00 E0 04 00 00 00 00 00 00 00 FE FF 00 E0 54 16 01 00 FF 4F FF 51 00 29 00 00 00 00 01 40 00 00 01 40 00 00 00 00 00 00 00 00 00 00 01 40 00 00 01 40 00 00 00 00 00 00 00 00 00 01 08 01 01 FF 52 00 0C 00 00 00 01 00 05 04 04 00 01 FF 5C 00 13 20 50 58 58 60 58 58 60 58 58 60 58 58 58 50 50 58
J2K codestream has 320x320, 1 component and 9-bit precision. What's the output from python -m pydicom.env_info
?
Ah, 9-bit. You're using Pillow, aren't you? There's an issue with their decoding of 9-bit JPEG 2000 codestreams that should be fixed in the next Pillow release. In the meantime you can switch to using GDCM or pylibjpeg (with the pylibjpeg-openjpeg plugin)
@darcymason Oh I missed your reply.
The output of python -m pydicom.env_info
is:
module | version
------ | -------
platform | Linux-6.5.0-28-generic-x86_64-with-glibc2.35
Python | 3.10.11 (main, Apr 20 2023, 19:02:41) [GCC 11.2.0]
pydicom | 2.3.1
gdcm | _module not found_
jpeg_ls | _module not found_
numpy | 1.26.4
PIL | 9.5.0
pylibjpeg | _module not found_
openjpeg | _module not found_
libjpeg | _module not found_
In the meantime you can switch to using GDCM or pylibjpeg
Ah, 9-bit. You're using Pillow, aren't you? There's an issue with their decoding of 9-bit JPEG 2000 codestreams that should be fixed in the next Pillow release. In the meantime you can switch to using GDCM or pylibjpeg (with the pylibjpeg-openjpeg plugin)
How can I "switch" to pylibjpeg
? I installed pylibjpeg by python -m pip install -U pylibjpeg-openjpeg
but the error is still there.
How can I tell pydicom to use another library?
With v2.4 you can do:
ds = dcmread(...)
ds.convert_pixel_array("pylibjpeg")
arr = ds.pixel_array
Also, I think I closed this by mistake, this is a separate issue to #2006
I got AttributeError: 'FileDataset' object has no attribute 'convert_pixel_array'
with pydicom==2.4.3
and pydicom==2.4.0
.
I'm using a 3rd party package to read the dicom files whose default behavior is ds.pixel_array
after dread.
Is there any way I can fix this globally without editing the source code of that package?
Ah, sorry, its convert_pixel_data()
I got this error:
RuntimeError: The pixel data handler 'pylibjpeg' is not available on your system. Please refer to the pydicom documentation for information on installing needed packages.
With those two dependencies installed
pip install "pylibjpeg[all]"
pip install pylibjpeg-libjpeg
Can you post the output from python -m pydicom.env_info
again?
@zeakey did you resolve your issue?