Python 3.3でTkinterとpython.boostを使用しています。
中央に画像を表示するシンプルな GUI を作成したいと考えています。イメージは、double 値を使用して C++ クラスに格納されます。tkinter を使用して画像を表示するには、 PhotoImageオブジェクトを作成する必要があります。
Image.fromstring(mode、size、data、decoder、parameters)メソッドを使用して PhotoImage オブジェクトを作成できることを読みました。私が考えているのは、バッファを std::string に変換する関数を作成すると、PhotoImage オブジェクトを作成できるということです。
私のC++クラスは次のとおりです。
class DoubleImage
{
double* m_dBuffer; // Values from 0 to 1
public:
DoubleImage(const char* fileName, const int width, const int height);
/* Class Functions */
std::string toString( void ) {
double *pSrc = m_dBuffer;
int size = getWidth() * getHeight() * getChannels();
char* buffer = new char[size];
char* pbuffer = buffer;
for ( int i = 0; i < size; ++i, ++pSrc, ++pbuffer ) {
*pbuffer = ((char) (*pSrc * 255.0));
}
std::string ret(buffer);
delete[] buffer;
return ret;
}
};
Pythonブーストコードは次のとおりです。
using namespace boost::python;
BOOST_PYTHON_MODULE(ImageModule)
{
class_<DoubleImage>("DoubleImage", init<const char*, const int, const int>())
.add_property("width", &DoubleImage::getWidth)
.add_property("height", &DoubleImage::getHeight)
.add_property("channels", &DoubleImage::getChannels)
// Some functions ...
.def("toString", &DoubleImage::toString)
;
}
しかし、Python で toString を実行すると、次のエラーが発生します。
>>> from ImageModule import *
>>> img = DoubleImage('001.png', 300, 200)
>>> buffer = img.toString()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x86 in position 155: invalid start byte
>>>
2 つの質問があります。まず、何が足りないのでしょうか?? 次に、このエラーを解決したら、PhotoImage オブジェクトを作成するためにどのパラメーターを使用する必要がありますか?