Skip to content

Commit b59704a

Browse files
committed
Fixed Demo 29
1 parent 0080c99 commit b59704a

File tree

2 files changed

+23
-40
lines changed

2 files changed

+23
-40
lines changed

Demos/Demo29/Unit1.dfm

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ object Form1: TForm1
22
Left = 218
33
Top = 18
44
Caption = 'Form1'
5-
ClientHeight = 809
5+
ClientHeight = 701
66
ClientWidth = 668
77
Color = clBtnFace
88
Font.Charset = DEFAULT_CHARSET
@@ -11,6 +11,9 @@ object Form1: TForm1
1111
Font.Name = 'MS Sans Serif'
1212
Font.Style = []
1313
OldCreateOrder = False
14+
DesignSize = (
15+
668
16+
701)
1417
PixelsPerInch = 96
1518
TextHeight = 13
1619
object Image1: TImage
@@ -39,10 +42,11 @@ object Form1: TForm1
3942
OnClick = Button2Click
4043
end
4144
object Memo1: TMemo
42-
Left = 8
43-
Top = 471
44-
Width = 657
45-
Height = 330
45+
Left = 0
46+
Top = 472
47+
Width = 668
48+
Height = 229
49+
Align = alBottom
4650
Font.Charset = DEFAULT_CHARSET
4751
Font.Color = clWindowText
4852
Font.Height = -13
@@ -69,20 +73,23 @@ object Form1: TForm1
6973
' new_im.format = im.format'
7074
' return new_im'
7175
' '
72-
'def ImageToString(image):'
76+
'def ImageToBytes(image):'
7377
' stream = BytesIO()'
7478
' image.save(stream, image.format)'
7579
' return stream.getvalue()')
7680
ParentFont = False
7781
ScrollBars = ssBoth
7882
TabOrder = 2
7983
WordWrap = False
84+
ExplicitLeft = 16
85+
ExplicitTop = 352
8086
end
8187
object Memo2: TMemo
8288
Left = 8
8389
Top = 352
8490
Width = 657
85-
Height = 113
91+
Height = 114
92+
Anchors = [akLeft, akTop, akRight]
8693
Font.Charset = DEFAULT_CHARSET
8794
Font.Color = clWindowText
8895
Font.Height = -13

Demos/Demo29/Unit1.pas

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -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;
7147
var
7248
_stream : TMemoryStream;
73-
_str : PPyObject;
49+
_bytes : PPyObject;
7450
begin
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

Comments
 (0)