Hi
I'm trying to write a simple script to convert a DICOM SC file into a JPEG image. It works when I run it the first time with one image but as soon as I try another image which has a slightly different size I get the following error:
Traceback (most recent call last):
File "dcmjpg.py", line 43, in <module>
plt.imshow(data.pixel_array)
File "C:\Anaconda\lib\site-packages\pydicom-1.0.0a1-py2.7.egg\pydicom\dataset.
py", line 678, in pixel_array
return self._get_pixel_array()
File "C:\Anaconda\lib\site-packages\pydicom-1.0.0a1-py2.7.egg\pydicom\dataset.
py", line 670, in _get_pixel_array
self._pixel_array = self._pixel_data_numpy()
File "C:\Anaconda\lib\site-packages\pydicom-1.0.0a1-py2.7.egg\pydicom\dataset.
py", line 509, in _pixel_data_numpy
pixel_array = pixel_array.reshape(self.Rows, self.Columns, self.SamplesPerPi
ValueError: total size of new array must be unchanged
If I run it again using the original image it works fine again. I am running it each time from the command line as:
python dcmjpg.py inputfile outputdir
I have seen this behavior on both my Windows 7 64 bit machine and an old XP 32bit machine both of which have an Anaconda 2.7 distro of Python installed on them.
The code itself up until the point which throws the error (line plt.imshow(data.pixel_array)) is very simple see below:
Function: Converts a DICOM SC file from Prosoma to a JPEG image file with some
annotation added to the image to aid in the identification of the
image and the appropriate association of the image.
Usage: dcmjpg input_dicom_file output_dir
import matplotlib.pyplot as plt
# Get the input filename from the command line and the output directory
data = dicom.read_file(dcmfile, force=True)
# Add in the PlanarConfiguration tag to the DICOM, if left out it should be
# assumed to be interlaced but imshow fails unless it is explicitly defined
data.add_new([0x0028,0x0006],'US',0)
# Create a figure based on the ColxRow of the DICOM data
XSize = data.Columns/100.
fig = plt.figure(figsize=(XSize,YSize),frameon=False,dpi=100)
# Set the plot area axes so that the image fills the whole figure with no border
# Remove any axis markers from the image
# Display the pixel data of the image
plt.imshow(data.pixel_array)
Any ideas what might be causing this problem? It almost seems as if there is some residual memory of the pixel_array size from one runtime to another runtime which is odd. Maybe I should be clearing something out or initializing something at the start to prevent this problem happening.
Any ideas would be most appreciated.
Regards
Andrew Hoole