ValueError: total size of new array must be unchanged

109 views
Skip to first unread message

Andrew Hoole

unread,
Sep 16, 2016, 8:24:01 AM9/16/16
to pydicom
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
xel)
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. 

Searching on google I found this link (https://travis-ci.org/darcymason/pydicom/jobs/141981764) which seems to highlight the same problem. 

The code itself up until the point which throws the error (line plt.imshow(data.pixel_array)) is very simple see below:

# -*- coding: utf-8 -*-
"""
Title: DcmJpg.py 
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

Written by: A. Hoole
Date: 14/9/2016

Version: 0.9
"""
import sys
import pydicom as dicom 
import matplotlib.pyplot as plt

# Get the input filename from the command line and the output directory
dcmfile = sys.argv[1]
outdir = sys.argv[2]

# Read in the dicom file 
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.
YSize = data.Rows/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
fig.add_axes([0,0,1,1])

# Remove any axis markers from the image
plt.xticks(())
plt.yticks(())

# 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

Darcy Mason

unread,
Sep 16, 2016, 12:26:56 PM9/16/16
to pydicom
I just had a quick read, but sounds the same as https://github.com/darcymason/pydicom/pull/271.  I believe there is a merge done with code there, and then again a further fix in PR#274. Perhaps see if that last commit solves your problem.

-Darcy

Andrew Hoole

unread,
Sep 19, 2016, 4:33:09 AM9/19/16
to pydicom
I'll give that a go. Do I just need to download and upgrade my installation of pydicom to the last commit. 

Regards

Andrew

Darcy Mason

unread,
Sep 19, 2016, 10:03:41 AM9/19/16
to pydicom
It might depend somewhat on how you are set up - e.g. virtual environment, etc., but probably the easiest way in any case is to use pip with a specific commit specified. 


Again, depending on your setup, I suppose you might need the upgrade flag.  I'm not sure if that still works as in that link above.  If all else fails you could set up a virtual environment and install that specific commit in the virtual environment.

-Darcy
Reply all
Reply to author
Forward
0 new messages