Image Generator OpenAI Delphi (5 / 5)

OpenAI API allows given a prompt and/or an input image, the model will generate a new image. The Images API provides three methods for interacting with images:

1. Creating images from scratch based on a text prompt
2. Creating edits of an existing image based on a new text prompt
3. Creating variations of an existing image

Image Generator Delphi Example

The OpenAI API has the following methods to create a new image using a prompt.

- prompt: (Required) A text description of the desired image(s). The maximum length is 1000 characters.

- n: The number of images to generate. Must be between 1 and 10.

- size: The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.

- response_format: The format in which the generated images are returned. Must be one of url or b64_json.

user: A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.


Find below a simple delphi example which generates an image given a prompt.

procedure TFRMOpenAIImages.DoCreateImage(const aPrompt: string);
var
  oRequest: TsgcOpenAIClass_Request_Image;
  oResponse: TsgcOpenAIClass_Response_Image;
  oStream: TBytesStream;
  oPNG : TPngImage;
begin
  oRequest := TsgcOpenAIClass_Request_Image.Create;
  Try
    oRequest.Prompt := aPrompt;
    oRequest.ResponseFormat := 'b64_json';
    oResponse := OpenAI.CreateImage(oRequest);
    Try
      if Length(oResponse.Data) > 0 then
      begin
        oStream := TBytesStream.Create(DecodeBase64(AnsiString(oResponse.Data[0].B64_json)));
        Try
          oPNG := TPngImage.Create;
          Try
            oPNG.LoadFromStream(oStream);
            Image1.Picture.Assign(oPNG);
          Finally
            oPNG.Free;
          End;
        Finally
          oStream.free;
        End;
      end;
    Finally
      oResponse.Free;
    End;
  Finally
    oRequest.Free;
  End;
end; 

Find below the compiled demo for windows built with the sgcWebSockets library. 

sgcOpenAI_Images
2.8 mb
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

sgcWebSockets 2023.3
sgcWebSockets 2023.2

Related Posts