Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
AI Modify Existing Image with Text Prompt
See more AI Examples
Uses an AI text prompt and uploaded image data to modify an existing image and receive the modified output.Chilkat Pascal (Lazarus/Delphi) Downloads
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;
bdImageData: TBinData;
ai: TAi;
askParams: TJsonObject;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Load the data from an existing image.
bdImageData := TBinData.Create;
success := bdImageData.LoadFile('qa_data/jpg/kid_blue_coat.jpg');
if (success = False) then
begin
WriteLn(bdImageData.LastErrorText);
Exit;
end;
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');
ai.SetAskParams(askParams);
ai.InputAddImageData(bdImageData,'');
ai.InputAddText('Modify the image by replacing the blue coat with a Metallica T-shirt.');
// Give the AI some time (2 minutes).
ai.IdleTimeoutMs := 120000;
// 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.;
success := ai.GetOutputBd(bdImageData);
if (success = False) then
begin
WriteLn(ai.LastErrorText);
Exit;
end;
bdImageData.WriteFile('c:/aaworkarea/out.jpg');
WriteLn('Success.');
// -------------------------------
// Sample Input:
// -------------------------------
//
// -------------------------------
// Sample Output:
// -------------------------------
//
bdImageData.Free;
ai.Free;
askParams.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.