title: How to replace pixel data in same DICOM file using pyDicom to read it again with any DICOM viewer?
body: <p>I want to read some DICOM files, so I'm testing <code>pydicom</code> for my work, which I think is considerably useful.</p>
  <p>Now I want to load existing DICOM files, replace the pixel data array with another pixel array (e.g. pre-processing or literally another DICOM pixel array) and most of all, I want to read it again with any DICOM viewer application.</p>
  <p>For this test, I used the tutorial code below. This code loads a test data file. The size of image is <code>64*64</code>. The code below does sub-sampling from the original data. After that, the size of image is <code>8*8</code>, and the result is saved to <code>after.dcm</code>.</p>
  <p>But when I read the file using a  DICOM viewer app (I used 'Dicompass'), the size of DICOM image is still <code>64*64</code>. What is it that I'm missing?</p>
  <p>I referred to the <code>pydicom</code> documentation (<a href="http://pydicom.readthedocs.io/en/stable/getting_started.html" rel="nofollow noreferrer">http://pydicom.readthedocs.io/en/stable/getting_started.html</a>, <a href="https://pydicom.github.io/pydicom/stable/index.html" rel="nofollow noreferrer">https://pydicom.github.io/pydicom/stable/index.html</a>) to solve my problem.</p>
  <pre><code># authors : Guillaume Lemaitre &lt;g.lemaitre58@gmail.com&gt;
  # license : MIT
  
  import pydicom
  from pydicom.data import get_testdata_files
  
  print(__doc__)
  
  # FIXME: add a full-sized MR image in the testing data
  filename = get_testdata_files('MR_small.dcm')[0]
  ds = pydicom.dcmread(filename)
  
  # get the pixel information into a numpy array
  data = ds.pixel_array
  print(data)
  
  print('The image has {} x {} voxels'.format(data.shape[0],
                                          data.shape[1]))
  data_downsampling = data[::8, ::8]
  print('The downsampled image has {} x {} voxels'.format(
      data_downsampling.shape[0], data_downsampling.shape[1]))
  
  # copy the data back to the original data set
  ds.PixelData = data_downsampling.tostring()
  # update the information regarding the shape of the data array
  ds.Rows, ds.Columns = data_downsampling.shape
  
  # print the image information given in the dataset
  print('The information of the data set after downsampling: \n')
  print(ds)
  print(ds.pixel_array)
  print(len(ds.PixelData))
  ds.save_as(&quot;after.dcm&quot;)
  </code></pre>