Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.BinData,
  Chilkat.Ai,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ai: TAi;
  askParams: TJsonObject;
  bdImageData: TBinData;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  ai := TAi.Create;

  ai.Provider := 'openai';

  //  Use your provider's API key.
  ai.ApiKey := 'MY_API_KEY';

  //  Choose a model.
  ai.Model := 'gpt-5';

  askParams := TJsonObject.Create;
  askParams.UpdateString('image.output_format','jpeg');
  askParams.UpdateString('image.size','1024x1024');
  askParams.UpdateString('image.quality','low');
  ai.SetAskParams(askParams);

  ai.InputAddText('Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf');

  //  Ask the AI for image output.
  success := ai.Ask('image');
  if (success = False) then
    begin
      WriteLn(ai.LastErrorText);
      Exit;
    end;

  //  Get the image response data.
  bdImageData := TBinData.Create;
  success := ai.GetOutputBd(bdImageData);
  if (success = False) then
    begin
      WriteLn(ai.LastErrorText);
      Exit;
    end;

  bdImageData.WriteFile('c:/aaworkarea/out.jpg');

  WriteLn('Success.');


  ai.Free;
  askParams.Free;
  bdImageData.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.