# ImageWriter

VTKExamples/Python/IO/ImageWriter

### Description

A generic function `WriteImage()` is provided that selects what image writer to use based on the file extenstion and then writes the render window to the file. The following formats are supported: BMP, JPEG, PNM, PNG, PostScript, TIFF.

If no file extension is specified, PNG is assumed.

The function `WriteImage()` is also available in the Snippets.

### Code

ImageWriter.py

    #!/usr/bin/env python

    import vtk

    def main():
        colors = vtk.vtkNamedColors()
        # Set the background color. Match those in VTKTextbook.pdf.
        bkg = map(lambda x: x / 256.0, [25, 51, 102])
        colors.SetColor("BkgColor", *bkg)

        # create a rendering window and renderer
        ren = vtk.vtkRenderer()
        renWin = vtk.vtkRenderWindow()
        renWin.AddRenderer(ren)

        # create a renderwindowinteractor
        iren = vtk.vtkRenderWindowInteractor()
        iren.SetRenderWindow(renWin)

        # create source
        source = vtk.vtkSphereSource()
        source.SetCenter(0, 0, 0)
        source.SetRadius(5.0)

        # mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(source.GetOutputPort())

        # actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

        # color the actor
        actor.GetProperty().SetColor(colors.GetColor3d("Yellow"))

        # assign actor to the renderer
        ren.AddActor(actor)
        ren.SetBackground(colors.GetColor3d("BkgColor"))

        renWin.Render()
        ext = ['', '.png', '.jpg', '.ps', '.tiff', '.bmp', '.pnm']
        filenames = list(map(lambda x: 'ImageWriter' + x, ext))
        filenames[0] = filenames[0] + '1'
        for f in filenames:
            WriteImage(f, renWin, rgba=False)

        iren.Initialize()
        iren.Start()

    def WriteImage(fileName, renWin, rgba=True):
        """
        Write the render window view to an image file.

        Image types supported are:
         BMP, JPEG, PNM, PNG, PostScript, TIFF.
        The default parameters are used for all writers, change as needed.

        :param fileName: The file name, if no extension then PNG is assumed.
        :param renWin: The render window.
        :param rgba: Used to set the buffer type.
        :return:
        """

        import os

        if fileName:
            # Select the writer to use.
            path, ext = os.path.splitext(fileName)
            ext = ext.lower()
            if not ext:
                ext = '.png'
                fileName = fileName + ext
            if ext == '.bmp':
                writer = vtk.vtkBMPWriter()
            elif ext == '.jpg':
                writer = vtk.vtkJPEGWriter()
            elif ext == '.pnm':
                writer = vtk.vtkPNMWriter()
            elif ext == '.ps':
                if rgba:
                    rgba = False
                writer = vtk.vtkPostScriptWriter()
            elif ext == '.tiff':
                writer = vtk.vtkTIFFWriter()
            else:
                writer = vtk.vtkPNGWriter()

            windowto_image_filter = vtk.vtkWindowToImageFilter()
            windowto_image_filter.SetInput(renWin)
            windowto_image_filter.SetScale(1)  # image quality
            if rgba:
                windowto_image_filter.SetInputBufferTypeToRGBA()
            else:
                windowto_image_filter.SetInputBufferTypeToRGB()
                # Read from the front buffer.
                windowto_image_filter.ReadFrontBufferOff()
                windowto_image_filter.Update()

            writer.SetFileName(fileName)
            writer.SetInputConnection(windowto_image_filter.GetOutputPort())
            writer.Write()
        else:
            raise RuntimeError('Need a filename.')

    if __name__ == '__main__':
        main()