1 | initial version |
To expose the shared python buffer from C++ side:
PyObject* :GetBufferPtr(Image img, long width, long height) { ..... Py_buffer pybuffer; //where buf points to a void* for image pixel int res = PyBuffer_FillInfo(&pybuffer, 0, buf, val, false, PyBUF_CONTIG); .. return PyMemoryView_FromBuffer(&pybuffer); }
To retrieve image buffer from Python's side and update the buffer from Python side: import myLib import cv2 import numpy as np imgPtr = myLib.GetImgBuffer(img1, cols1, rows1) numpy_array = np.frombuffer(imgPtr, dtype=np.uint8) //for one byte integer image numpy_array.shape = (rows1, cols1) //change the image in python dst = np.ones((rows1,cols1), dtype=np.uint8)
//update data in the buffer np.copyto(numpy_array, dst) //update the buffer numpy_array.shape = dst.shape
**Now, if you refresh your display in the C++ app, you will see the updated change from Python
Have fun coding!
2 | No.2 Revision |
To expose the shared python buffer from C++ side:
PyObject* :GetBufferPtr(Image img, long width, long height)
{ .....
Py_buffer pybuffer; //where buf points to a void* for image pixel
int res = PyBuffer_FillInfo(&pybuffer, 0, buf, val, false, PyBUF_CONTIG);
..
return PyMemoryView_FromBuffer(&pybuffer);
To retrieve image buffer from Python's side and update the buffer from Python side:
import myLib
import cv2
import numpy as np
imgPtr = myLib.GetImgBuffer(img1, cols1, rows1)
numpy_array = np.frombuffer(imgPtr, dtype=np.uint8) //for one byte integer image
numpy_array.shape = (rows1, cols1)
//change the image in python
dst = np.ones((rows1,cols1), **Now, if you refresh your display in the C++ app, you will see the updated change from Python
Have fun coding!