Exemple #1
0
 def __init__(self, module_manager):
     SimpleVTKClassModuleBase.__init__(
         self, module_manager,
         vtk.vtkPNMWriter(), 'Writing vtkPNM.',
         ('vtkPNM',), (),
         replaceDoc=True,
         inputFunctions=None, outputFunctions=None)
Exemple #2
0
def WriteImage(fileName, renWin1, 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 renWin1: 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(renWin1)
        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.')
Exemple #3
0
 def _get_image_writer(image_type: ImageTypes) -> None:
     """Get vtk image writer for each image type."""
     if image_type == ImageTypes.png:
         writer = vtk.vtkPNGWriter()
     elif image_type == ImageTypes.bmp:
         writer = vtk.vtkBMPWriter()
     elif image_type == ImageTypes.jpg:
         writer = vtk.vtkJPEGWriter()
     elif image_type == ImageTypes.pnm:
         writer = vtk.vtkPNMWriter()
     elif image_type == ImageTypes.ps:
         writer = vtk.vtkPostScriptWriter()
     elif image_type == ImageTypes.tiff:
         writer = vtk.vtkTIFFWriter()
     else:
         raise ValueError(f'Invalid image type: {image_type}')
     return writer
Exemple #4
0
 def ExportToImageFile(self, filename, format):
     if format == "tif":
         writer = vtk.vtkTIFFWriter()
     elif format == "bmp":
         writer = vtk.vtkBMPWriter()
     elif format == "jpg":
         writer = vtk.vtkJPEGWriter()
     elif format == "png":
         writer = vtk.vtkPNGWriter()
     elif format == "pnm":
         writer = vtk.vtkPNMWriter()
     else:
         raise Exception("Unsupported export format")
     imageFilter = vtk.vtkWindowToImageFilter()
     imageFilter.SetInput(self.GetRenderWindow())
     writer.SetInputConnection(imageFilter.GetOutputPort())
     writer.SetFileName(filename)
     self.GetRenderWindow().Render()
     writer.Write()
def saveRendererToImg(renderer,fnOut,magnification=10):
	
	"""Saves renderer to image file.
	
	Supported extensions are:
		
		* '.ps'  (PostScript)
		* '.eps' (Encapsualted PostScript)
		* '.pdf' (Portable Document Format)
		* '.jpg' (Joint Photographic Experts Group)
		* '.png' (Portable Network Graphics)
		* '.pnm' (Portable Any Map)
		* '.tif' (Tagged Image File Format)
		* '.bmp' (Bitmap Image)
	
	Some code taken from http://www.programcreek.com/python/example/23102/vtk.vtkGL2PSExporter .
	
	Args:
		fnOut (str): Path to output file.
		renderer (vtk.vtkOpenGLRenderer): Renderer.
		
	Keyword Args:
		magnification (int): Degree of magnification.
		
	Returns:
		vtk.vtkExporter: Exporter object.
	
	"""
	
	
	#Generate exporter
	vtkImageWriters = {
		'.tif': vtk.vtkTIFFWriter(),
		'.tiff': vtk.vtkTIFFWriter(),
		'.bmp': vtk.vtkBMPWriter(),
		'.pnm': vtk.vtkPNMWriter(),
		'.png': vtk.vtkPNGWriter(),
		'.jpg': vtk.vtkJPEGWriter(),
		'.jpeg': vtk.vtkJPEGWriter(),
		'.ps': vtk.vtkPostScriptWriter(),
		'.eps': vtk.vtkPostScriptWriter(),  
		}
	
	#Get extension
	basename,ext=os.path.splitext(fnOut)

	#Large Image renderer for nicer images
	rendererLarge=vtk.vtkRenderLargeImage()
	rendererLarge.SetInput(renderer)
	rendererLarge.SetMagnification(magnification)

	#Get proper writer
	try:
		writer = vtkImageWriters[ext.lower()]
	except KeyError:
		printError("Extension "+ext+" is currently not supported")
		return None
	
	#Write
	writer.SetFileName(fnOut)
	
	writer.SetInputConnection(rendererLarge.GetOutputPort())
	writer.Write()

	return writer
    def save_pnm(self, filename):
        """
		Save the rendered screen as png
		"""
        self.saveScreen(vtk.vtkPNMWriter(), filename)
Exemple #7
0
    def save_pnm(self, filename):
        """
		Save the rendered screen as png
		"""
        self.saveScreen(vtk.vtkPNMWriter(), filename)
Exemple #8
0
if (catch.catch(globals(),"""channel = open("" + str(dir) + "/test.tmp", "w")""") == 0):
    channel.close()
    file.delete("-force", "" + str(dir) + "/test.tmp")
    tiff1 = vtk.vtkTIFFWriter()
    tiff1.SetInputConnection(image1.GetOutputPort())
    tiff1.SetFileName("" + str(dir) + "/tiff1.tif")
    tiff2 = vtk.vtkTIFFWriter()
    tiff2.SetInputConnection(luminance.GetOutputPort())
    tiff2.SetFileName("" + str(dir) + "/tiff2.tif")
    bmp1 = vtk.vtkBMPWriter()
    bmp1.SetInputConnection(image1.GetOutputPort())
    bmp1.SetFileName("" + str(dir) + "/bmp1.bmp")
    bmp2 = vtk.vtkBMPWriter()
    bmp2.SetInputConnection(luminance.GetOutputPort())
    bmp2.SetFileName("" + str(dir) + "/bmp2.bmp")
    pnm1 = vtk.vtkPNMWriter()
    pnm1.SetInputConnection(image1.GetOutputPort())
    pnm1.SetFileName("" + str(dir) + "/pnm1.pnm")
    pnm2 = vtk.vtkPNMWriter()
    pnm2.SetInputConnection(luminance.GetOutputPort())
    pnm2.SetFileName("" + str(dir) + "/pnm2.pnm")
    psw1 = vtk.vtkPostScriptWriter()
    psw1.SetInputConnection(image1.GetOutputPort())
    psw1.SetFileName("" + str(dir) + "/psw1.ps")
    psw2 = vtk.vtkPostScriptWriter()
    psw2.SetInputConnection(luminance.GetOutputPort())
    psw2.SetFileName("" + str(dir) + "/psw2.ps")
    pngw1 = vtk.vtkPNGWriter()
    pngw1.SetInputConnection(image1.GetOutputPort())
    pngw1.SetFileName("" + str(dir) + "/pngw1.png")
    pngw2 = vtk.vtkPNGWriter()
Exemple #9
0
             """channel = open("" + str(dir) + "/test.tmp", "w")""") == 0):
 channel.close()
 file.delete("-force", "" + str(dir) + "/test.tmp")
 tiff1 = vtk.vtkTIFFWriter()
 tiff1.SetInputConnection(image1.GetOutputPort())
 tiff1.SetFileName("" + str(dir) + "/tiff1.tif")
 tiff2 = vtk.vtkTIFFWriter()
 tiff2.SetInputConnection(luminance.GetOutputPort())
 tiff2.SetFileName("" + str(dir) + "/tiff2.tif")
 bmp1 = vtk.vtkBMPWriter()
 bmp1.SetInputConnection(image1.GetOutputPort())
 bmp1.SetFileName("" + str(dir) + "/bmp1.bmp")
 bmp2 = vtk.vtkBMPWriter()
 bmp2.SetInputConnection(luminance.GetOutputPort())
 bmp2.SetFileName("" + str(dir) + "/bmp2.bmp")
 pnm1 = vtk.vtkPNMWriter()
 pnm1.SetInputConnection(image1.GetOutputPort())
 pnm1.SetFileName("" + str(dir) + "/pnm1.pnm")
 pnm2 = vtk.vtkPNMWriter()
 pnm2.SetInputConnection(luminance.GetOutputPort())
 pnm2.SetFileName("" + str(dir) + "/pnm2.pnm")
 psw1 = vtk.vtkPostScriptWriter()
 psw1.SetInputConnection(image1.GetOutputPort())
 psw1.SetFileName("" + str(dir) + "/psw1.ps")
 psw2 = vtk.vtkPostScriptWriter()
 psw2.SetInputConnection(luminance.GetOutputPort())
 psw2.SetFileName("" + str(dir) + "/psw2.ps")
 pngw1 = vtk.vtkPNGWriter()
 pngw1.SetInputConnection(image1.GetOutputPort())
 pngw1.SetFileName("" + str(dir) + "/pngw1.png")
 pngw2 = vtk.vtkPNGWriter()
Exemple #10
0
def saveRendererToImg(renderer, fnOut, magnification=10):
    """Saves renderer to image file.
	
	Supported extensions are:
		
		* '.ps'  (PostScript)
		* '.eps' (Encapsualted PostScript)
		* '.pdf' (Portable Document Format)
		* '.jpg' (Joint Photographic Experts Group)
		* '.png' (Portable Network Graphics)
		* '.pnm' (Portable Any Map)
		* '.tif' (Tagged Image File Format)
		* '.bmp' (Bitmap Image)
	
	Some code taken from http://www.programcreek.com/python/example/23102/vtk.vtkGL2PSExporter .
	
	Args:
		fnOut (str): Path to output file.
		renderer (vtk.vtkOpenGLRenderer): Renderer.
		
	Keyword Args:
		magnification (int): Degree of magnification.
		
	Returns:
		vtk.vtkExporter: Exporter object.
	
	"""

    #Generate exporter
    vtkImageWriters = {
        '.tif': vtk.vtkTIFFWriter(),
        '.tiff': vtk.vtkTIFFWriter(),
        '.bmp': vtk.vtkBMPWriter(),
        '.pnm': vtk.vtkPNMWriter(),
        '.png': vtk.vtkPNGWriter(),
        '.jpg': vtk.vtkJPEGWriter(),
        '.jpeg': vtk.vtkJPEGWriter(),
        '.ps': vtk.vtkPostScriptWriter(),
        '.eps': vtk.vtkPostScriptWriter(),
    }

    #Get extension
    basename, ext = os.path.splitext(fnOut)

    #Large Image renderer for nicer images
    rendererLarge = vtk.vtkRenderLargeImage()
    rendererLarge.SetInput(renderer)
    rendererLarge.SetMagnification(magnification)

    #Get proper writer
    try:
        writer = vtkImageWriters[ext.lower()]
    except KeyError:
        printError("Extension " + ext + " is currently not supported")
        return None

    #Write
    writer.SetFileName(fnOut)

    writer.SetInputConnection(rendererLarge.GetOutputPort())
    writer.Write()

    return writer