VTK export JPG from DICOM

Exporting .jpg image copy from .dcm input using VTK in Python.

# Iterate through .dcm files and export to .jpg
for image in os.listdir(PathDicom):
if (image.endswith(".dcm")):
reader.SetFileName(image)
reader.Update()

# Need to cast before writing to .jpg
castFilter = vtk.vtkImageCast()
castFilter.SetOutputScalarTypeToUnsignedChar()
castFilter.SetInputConnection(reader.GetOutputPort())
castFilter.Update()

# .jpg file write
writer = vtk.vtkJPEGWriter()
writer.SetInputConnection(castFilter.GetOutputPort())
jpg_filename = image.replace(“.dcm”, “.jpg”)
writer.SetFileName(“{0}”.format(jpg_filename))
writer.Write()

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s