Turning ctypes data into a python string as fast as possible

I am trying to write a video application in PyQt4, and I used Python types to connect to the old generation video decoder library. The library gives me 32-bit ARGB data, and I need to turn this into a QImage. It works for me as follows:


# Copy the rgb image data from the pointer into the buffer
memmove(self.rgb_buffer, self.rgb_buffer_ptr, self.buffer_size)

# Copy the buffer to a python string
imgdata = ""
for a in self.rgb_buffer:
    imgdata = imgdata + a

# Create a QImage from the string data
img = QImage(imgdata, 720, 288, QImage.Format_ARGB32)

The problem is that ctypes displays the data as a type " ctypes.c_char_Array_829440", and I need to turn it into a python string so that I can build a QImage. Currently, my copy engine takes almost 300 ms per image, so it is very slow. Part of the decoding and display of the process takes only about 50 ms.

Can someone think of some tricky shortcuts that I can take to speed up this process and avoid having to copy the buffer twice, as I am doing now?

+3
source share
1 answer

An instance ctypes.c_char_Array_829400has a property .rawthat returns a string that can contain NUL bytes, and a property .valuethat returns a string up to the first NUL bytes if it contains one or more.

However, you can also use ctypes to access the line in self.rgb_buffer_ptr, for example ctypes.string_at(self.rgb_buffer_ptr, self.buffer_size); this avoids the need to call memmove.

+6
source

Source: https://habr.com/ru/post/1705871/


All Articles