Sunday, December 18, 2022

Playing with AI Delphi code generation

Inspired by Michalis, I have been playing with the Open AI chat bot today:

https://chat.openai.com

 

It's early days, I suppose, but my results were decidedly mixed:

 

Query 1: How can I display thumbnails of images using Delphi?

 

Result:

To display thumbnails of images using Delphi, you can use the TImageEnMView component from the ImageEn library. This component is designed specifically for displaying and managing a collection of images, including the ability to display thumbnails of the images.

Here is an example of Delphi code that demonstrates how to use the TImageEnMView component to display thumbnails of images:

uses ImageEnMView, Graphics; var ImageEnMView: TImageEnMView; Bitmap: TBitmap; i: Integer; begin ImageEnMView := TImageEnMView.Create(self); try // Set the thumbnail size ImageEnMView.ThumbWidth := 128; ImageEnMView.ThumbHeight := 128; // Add the images to the TImageEnMView for i := 0 to ImageCount - 1 do begin Bitmap := TBitmap.Create; try // Load the image from a file or stream Bitmap.LoadFromFile(ImageFilename[i]); // Add the image to the TImageEnMView ImageEnMView.IO.AddGraphic(Bitmap); finally Bitmap.Free; end; end; // Display the TImageEnMView on the form ImageEnMView.Parent := Self; ImageEnMView.Align := alClient; finally ImageEnMView.Free; end; end;

This code creates a new TImageEnMView component and sets the ThumbWidth and ThumbHeight properties to specify the size of the thumbnails. It then loads the images from a file or stream and adds them to the TImageEnMView using the AddGraphic method. Finally, it displays the TImageEnMView on the form by assigning it to the Parent property and setting the Align property to alClient.

You can customize the appearance of the thumbnails by setting various properties of the TImageEnMView component, such as the background color, border style, and spacing between the thumbnails. You can also handle events of the TImageEnMView component, such as OnThumbClick, to perform actions when the user clicks on a thumbnail.

 

Analysis:

So it's not a bad result for an engine. It points the user to a valid component for it  (our TImageEnMView *Blush*), and some of the code is correct, but is certainly won't work. Firstly, if you use a TBitmap you'd be limited to the formats that it supports, and more importantly TImageEnMView does not have an IO property or AddGraphic method.

 The correct code would be:

uses
  IEMView, Graphics;

var
  ImageEnMView: TImageEnMView;
  Bitmap: TBitmap;
  i: Integer;
begin
  ImageEnMView := TImageEnMView.Create(self);
  try
    // Set the thumbnail size
    ImageEnMView.ThumbWidth := 128;
    ImageEnMView.ThumbHeight := 128;

    // Add the images to the TImageEnMView
    for i := 0 to ImageCount - 1 do
      ImageEnMView.AppendImage(ImageFilename[i]);

    // Display the TImageEnMView on the form
    ImageEnMView.Parent := Self;
    ImageEnMView.Align := alClient;
  finally
    ImageEnMView.Free;
  end;
end;

 

 Query 2: How can I display a PSD image with Delphi?

 

 Result

To display a PSD (Photoshop Document) file with Delphi, you can use the TImage component from the VCL (Visual Component Library) or the TImageEnView component from the ImageEn library. Both of these components include support for displaying a wide variety of image formats, including PSD.

Here is an example of Delphi code that demonstrates how to use the TImage component to display a PSD file:

uses Graphics; var Bitmap: TBitmap; FileName: string; begin FileName := 'C:\path\to\image.psd'; Bitmap := TBitmap.Create; try // Load the PSD image from a file Bitmap.LoadFromFile(FileName); // Display the image on the TImage component Image.Picture.Assign(Bitmap); finally Bitmap.Free; end; end;

This code creates a new TBitmap object and uses the LoadFromFile method to load the PSD image from a file. It then assigns the TBitmap object to the Picture property of the TImage component to display the image.

Alternatively, you can use the TImageEnView component from the ImageEn library to display the PSD image. The TImageEnView component provides additional features and capabilities for displaying and manipulating images, such as support for zooming and panning. Here is an example of Delphi code that demonstrates how to use the TImageEnView component to display a PSD file:

uses ImageEnView; var ImageEnView: TImageEnView; FileName: string; begin FileName := 'C:\path\to\image.psd'; ImageEnView := TImageEnView.Create(self); try // Load the PSD image from a file ImageEnView.IO.LoadFromFile(FileName); // Display the image on the TImageEnView component ImageEnView.Parent := Self; ImageEnView.Align := alClient; finally ImageEnView.Free; end; end;

This code creates a new TImageEnView object and uses the LoadFromFile method of the IO property to load the PSD image from a file. It then displays the TImageEnView component on the form by assigning it to the Parent property and setting the Align property to alClient.

  

Analysis:

I don't believe it is possible to load PSD files with TImage, so I guess that part is all wrong. The ImageEn code on the other hand, is spot on (though it would obviously easier just to a TImageEnView component to your form at design time).


Query 3: Suggest a Delphi component that allows user to select an area of the image


Result (Second attempt):

One Delphi component that you can use to allow users to select a region of an image is the TImageEnView component from the ImageEn library.

The TImageEnView component is a single-image view that displays an image and allows users to select a region of the image using various selection modes. You can use it to allow the user to select a region of an image using the mouse or other input device.

Here is an example of how you can use the TImageEnView component to allow users to select a region of an image in Delphi:

uses ImageEn; var ImageEnView: TImageEnView; Selection: TRect; begin ImageEnView := TImageEnView.Create(Self); ImageEnView.Parent := Self; ImageEnView.Align := alClient; ImageEnView.SelectionMode := smRectangle; ImageEnView.ShowSelectionRect := True; // Load an image into the TImageEnView component ImageEnView.IO.LoadFromFile('image.jpg'); // Wait for the user to select a region of the image while ImageEnView.SelectionCount = 0 do begin Application.ProcessMessages; Sleep(10); end; // Get the selected region of the image Selection := ImageEnView.SelectionRect[0]; end;

This code creates a new instance of the TImageEnView component, sets the SelectionMode property to allow the user to select a region of the image using the mouse, and loads an image into the component. It then waits for the user to select a region of the image and retrieves the selected region using the SelectionRect property.

I hope this helps! Let me know if you have any questions.

 

Analysis

So the first result offered a TImageEnMView (our control that displays a grid of images) when really a TImageEnView is needed (displays a single image that supports selection).

So I clicked the "Regenerate Response" button and the second option was for TImageEnView. OK, good, but the code is pretty iffy. There are a few properties that are invalid, and it's almost a console type solution anyway (blocking the code while waiting for a selection).

Working code would be (persisting with the same blocking approach):

uses
  ImageEn;

var
  ImageEnView: TImageEnView;
  Selection: TRect;
begin
  ImageEnView := TImageEnView.Create(Self);
  ImageEnView.Parent := Self;
  ImageEnView.Align := alClient;
  ImageEnView.MouseInteractGeneral := [miSelect];

  // Load an image into the TImageEnView component
  ImageEnView.IO.LoadFromFile('D:\im.jpg');

  // Wait for the user to select a region of the image
  // NOTE: Normally you would just use the OnSelectionChange event,
  // not a forever loop
  while ( ImageEnView.Selected = False ) or IEIsLeftMouseButtonPressed() do
  begin
    Application.ProcessMessages;
    Sleep(10);
  end;

  // Get the selected region of the image
  ShowMessage( IERectangleToStr( ImageEnView.SelectedRect ));
end; 


No comments: