Create a PhotoImage with a C++ Buffer using: Python 3.3, Python.Boost and Tkinter Create a PhotoImage with a C++ Buffer using: Python 3.3, Python.Boost and Tkinter tkinter tkinter

Create a PhotoImage with a C++ Buffer using: Python 3.3, Python.Boost and Tkinter


Finally I found the solution after suffering a lot :P

First the function toString have to be changed like this:

std::string DoubleImage::toString( void ) {    ECV_CHECK_ERROR ( m_eType == IT_UKNOWN, "The image is not initialized!" );    double *pSrc  = (double *) m_cvImage.data;    long size = getWidth() * getHeight() * getChannels();    std::string ret(size, ' ');    for ( long i = 0; i < size; ++i, ++pSrc ) {         ret[i] = ((char) (*pSrc * 255.0));    }    return ret;}

otherwise, the resultant std::string wont have the total length (because the constructor search for the first '\0'). In addition the strange error "UnicodeDecodeError", only apears using python 3.3. By using python 2.7 the problem is solved. So that, I recommend using python 2.7 for image processing.

Then I have installed the PIL module (also only available on python 2.7). Then in python I create the PhotoImage object using this code:

>>> from ImageModule import *>>> from PIL import image>>> from ImageTk import PhotoImage>>> img = DoubleImage('001.png', 300, 200)>>> buffer = img.toString()>>> img_pil = Image.fromstring('RGB', [300, 200], buffer, 'raw', 'BGR', 300 * 3, 0)>>> tk_image = PhotoImage(img_pil)

where 300 is the image with, 200 the image height, RGB the ouput format, BGR the input format (IplImage) and 3 are the channels (3 * 300 the image step). Then all work like a charm :)