Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Buffer function for python 3+

I'm trying to open a vtk window using vtk_show, but my Ipython console crashes every time i do this, apparently this is because Ipython can't display an external window, which is exactly what vtk_show does. I searched on google for a solution, but it's written for python2 (i'm using python 3.6.3). Here's the solution i found:

import vtk
from IPython.display import Image

def vtk_show(renderer, width=400, height=300):
    """
    Takes vtkRenderer instance and returns an IPython Image with the 
    rendering.
    """
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.SetOffScreenRendering(1)
    renderWindow.AddRenderer(renderer)
    renderWindow.SetSize(width, height)
    renderWindow.Render()

    windowToImageFilter = vtk.vtkWindowToImageFilter()
    windowToImageFilter.SetInput(renderWindow)
    windowToImageFilter.Update()

    writer = vtk.vtkPNGWriter()
    writer.SetWriteToMemory(1)
    writer.SetInputConnection(windowToImageFilter.GetOutputPort())
    writer.Write()
    data = str(buffer(writer.GetResult()))

    return Image(data)

I'm getting an error while trying to use the buffer built-in function of python2, but as this function doesn't exist on python3+ i'm stuck.. If anyone could help me with this i would be very appreciated. Thanks in advance!

like image 257
tomas-silveira Avatar asked May 03 '18 16:05

tomas-silveira


People also ask

What is the buffer function Python?

Buffer structures (or simply “buffers”) are useful as a way to expose the binary data from another object to the Python programmer. They can also be used as a zero-copy slicing mechanism. Using their ability to reference a block of memory, it is possible to expose any data to the Python programmer quite easily.

How do I create a memory buffer in Python?

In Python, you don't have to care about memory management. You don't need to reserve "buffers", just assign the variables you want, and use them. If you assign too many (for your RAM), you might run into MemoryError s, indicating you don't have enough memory. You could use a Queue as some kind of buffer, though, e.g.

What is buffer size Python?

The buffer size tells how much data can be held at a time until it is used. io. DEFAULT_BUFFER_SIZE can tell the default buffer size of your platform. Optionally, you can pass an integer to buffering to set the buffering policy: 0 to switch off buffering (only allowed in binary mode)

What is a buffer NumPy?

The Numpy frombuffer() is one of the predefined function that is used to create the array using the buffer storage with specific areas; mainly, this buffer function is creating the arrays with a different set of parameters it returns the array version of the buffer the python interpreter of the numpy frombuffer() ...


1 Answers

At least these two points must be modified on your code to have the same behavior with Python 3:

  • The buffer(...) built-in function in Python 2 has been replaced by memoryview(...) in Python 3: What is Python buffer type for?. Replace the buffer call by memoryview
  • the str(...) built-in function has to replaced by a bytes(...) call to get a bytes object: https://docs.python.org/2/howto/pyporting.html#text-versus-binary-data

So the data = ... line should read:

data = bytes(memoryview(writer.GetResult()))
like image 172
jcgiret Avatar answered Oct 05 '22 13:10

jcgiret