title: Create pydicom file from numpy array
body: <p>I'm trying to create a new dicom image from a standard-sized (512 x 512 or 256 x 256) numpy array.</p>
  <pre><code>import dicom, dicom.UID
  from dicom.dataset import Dataset, FileDataset
  
  def write_dicom(pixel_array,filename):
      
      file_meta = Dataset()
      ds = FileDataset(filename, {},file_meta = file_meta,preamble=&quot;\0&quot;*128)
      ds.PixelData = pixel_array.tostring()
      ds.save_as(filename)
      return
  
  if __name__ == &quot;__main__&quot;:
      import numpy as np
      pixel_array = np.tile(np.arange(256).reshape(16,16), (16,16)) * 4
      write_dicom(pixel_array,'pretty.dcm')
  </code></pre>