@@ -43,41 +43,17 @@ implementation
4343
4444{ $R *.dfm}
4545
46- // Old code not compatible with Unicode
47- //
48- // function ImageToString(AGraphic : TGraphic) : String;
49- // var
50- // _stream : TStringStream;
51- // begin
52- // _stream := TStringStream.Create('');
53- // try
54- // AGraphic.SaveToStream(_stream);
55- // Result := _stream.DataString;
56- // finally
57- // _stream.Free;
58- // end;
59- // end;
60- //
61- // function BinStrToPyStr(const AString : String) : Variant;
62- // var
63- // _str : PPyObject;
64- // begin
65- // _str := GetPythonEngine.PyString_FromStringAndSize(PAnsiChar(AString), Length(AString)*SizeOf(Char));
66- // Result := VarPythonCreate(_str);
67- // GetPythonEngine.Py_DECREF(_str);
68- // end;
69-
70- function ImageToPyStr (AGraphic : TGraphic) : Variant;
46+ function ImageToPyBytes (AGraphic : TGraphic) : Variant;
7147var
7248 _stream : TMemoryStream;
73- _str : PPyObject;
49+ _bytes : PPyObject;
7450begin
7551 _stream := TMemoryStream.Create();
7652 try
7753 AGraphic.SaveToStream(_stream);
78- _str := GetPythonEngine.PyString_FromStringAndSize (_stream.Memory, _stream.Size);
79- Result := VarPythonCreate(_str );
80- GetPythonEngine.Py_DECREF(_str );
54+ _bytes := GetPythonEngine.PyBytes_FromStringAndSize (_stream.Memory, _stream.Size);
55+ Result := VarPythonCreate(_bytes );
56+ GetPythonEngine.Py_DECREF(_bytes );
8157 finally
8258 _stream.Free;
8359 end ;
@@ -102,17 +78,17 @@ procedure TForm1.Button2Click(Sender: TObject);
10278 if (Image1.Picture.Graphic = nil ) or Image1.Picture.Graphic.Empty then
10379 raise Exception.Create(' You must first select an image' );
10480 PythonEngine1.ExecStrings(Memo1.Lines);
105- _im := MainModule.ProcessImage(ImageToPyStr (Image1.Picture.Graphic));
81+ _im := MainModule.ProcessImage(ImageToPyBytes (Image1.Picture.Graphic));
10682 if not chkUseDC.Checked then
10783 begin
10884 // We have to call PyString_AsStringAndSize because the image may contain zeros
10985 with GetPythonEngine do begin
11086 pargs := MakePyTuple([ExtractPythonObjectFrom(_im)]);
11187 try
88+ presult := PyEval_CallObjectWithKeywords(
89+ ExtractPythonObjectFrom(MainModule.ImageToBytes), pargs, nil );
11290 try
113- presult := PyEval_CallObjectWithKeywords(
114- ExtractPythonObjectFrom(MainModule.ImageToString), pargs, nil );
115- if (PyString_AsStringAndSize(presult, P, Len) < 0 ) or (P = nil ) then begin
91+ if (P = nil ) or (PyBytes_AsStringAndSize(presult, P, Len) < 0 ) then begin
11692 ShowMessage(' This does not work and needs fixing' );
11793 Abort;
11894 end ;
0 commit comments