title: Convert DICOM image using VTK
body: <p>I need to make a conversion from a DICOM image to a JPG/PNG and save the image using VTK, but the image that I produce does not match the original.</p>
  
  <p><a href="https://i.stack.imgur.com/i840i.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i840i.jpg" alt="enter image description here"></a></p>
  
  <p>I know I need rescaling the pixels of the image to convert it but I do not know how. Does anyone know how I can do the conversion properly?</p>
  
  <p>Below, my code in python:</p>
  
  <pre><code>from vtk import *
  
  reader = vtkDICOMImageReader()
  reader.SetFileName('image.dcm')
  reader.Update()
  
  castFilter = vtkImageCast()
  castFilter.SetOutputScalarTypeToUnsignedChar()
  castFilter.SetInputConnection(reader.GetOutputPort())
  castFilter.Update()
  
  writer = vtkJPEGWriter()
  writer.SetFileName('output.jpg')
  writer.SetInputConnection(castFilter.GetOutputPort())
  writer.Write()
  </code></pre>