Resource

Resource Basics

Resource Name AI Extract Handwritten Text with AI
Resource Version 1.0
Category Artificial Intelligence
Downloads 167
Release Date 3/3/2026 10:54:03 PM
 

Resource Details

Description This script will extract any handwritten text for an image and write that as the description in XMP.
Installation Instructions n.a.
Usage Instructions 1. Select one or more thumbnails that contain handwritten text on it
2. Right click on a selected thumb and select -> Run Script from Repository -> Extract Handwritten Text with AI
Changes in this version n.a.
Special Requirements
 

Author Information

Author Name IDimager Support
 

Script

var
  AAI: TidBaseOpenAI;

  function ProcessTextForImage(AImg: TImageItem; AText: WideString): Boolean;
  var
    ACatItem: TCatalogItem;
    AXmp: TXMP;
  begin
    result := False;

    ACatItem := TCatalogItem.Create(nil);
    AXmp := TXMP.Create(False);
    try
      if PublicCatalog.FindItemCombinedByGUID(AImg.GUID, ACatItem) then
      begin
        PublicCatalog.LoadXMPForItem(ACatItem, AXmp, PublicOptions.CachedXMP);

        AXmp.QuickSetProperty('http://purl.org/dc/elements/1.1/', 'dc:description', AText);

        PublicCatalog.SaveXMPForItem(ACatItem, AXmp, PublicOptions.CachedXMP);

        result := True;
      end;
    finally
      AXmp.Free;
      ACatItem.Free;
    end;
  end;

  function HandleImage(AImg: TImageItem): Boolean;
  var
    AMs: TMemoryStream;
    AResponse: TMemoryStream;
    AText: WideString;
  begin
    APrompt := 'Only return the recognized handwritten text in this image (encoded as UTF8). Ignore the rest everything else!';

    result := False;

    AMs := TMemoryStream.Create;
    AResponse := TMemoryStream.Create;
    try
      if PublicThumbnailer.LoadThumbStream(AImg.GUID, AMs) then
      begin
        AMs.Position := 0;

        AAI.Worker.CompletionsForImageStream(
          AMs, 
          FileMimeType(AImg.FileName), 
          APrompt,
          'json', 
          AResponse,
          0
        )

        AText := UTF8Decode(AResponse.AsAnsiString);

        // any text found?
        if Length(AText) > 0 then
        begin
          result := ProcessTextForImage(AImg, AText);
        end;
      end;
    finally
      AResponse.Free;
      AMs.Free;
    end;
  end;

var
  AImg: TImageItem;
  i, AHit: Integer;
  AProgress: TxomProgress;
begin
  if StrToInt(LeftStr(xomProductVersion, WideTextPos('.', xomProductVersion) - 1)) < '2026' then
  begin
    Say('This script requires Photo Supreme v2026 or higher.');
    exit;
  end;

  if Selected.Count = 0 then
  begin
    Say('Please select at least one thumbnail.');
    exit;
  end;
 
  if not AskYN('Are you sure that you want to start recognizing text for the ' + IntToStrW(Selected.Count) + ' selected images?' + CrLf2W + 'This will send the selected thumbnails to your configured AI service so it can extract the text and write it as the Description.') then
  begin
    exit;
  end;

  AHit := 0;

  AProgress := TxomProgress.Create(nil);
  AAI := PublicCatalog.CreateOpenAIObject;
  try
    AProgress.Caption := 'Extract Handwritten Text with AI';
    AProgress.CanCancel := True;
    AProgress.Max := Selected.Count;

    for i := 0 to Selected.Count - 1 do
    begin
      if AProgress.Cancel then
        break;

      AProgress.Pos := i + 1;
      if HandleImage(Selected.Items[i]) then
        Inc(AHit);
    end;
  finally
    AAI.Free;
    AProgress.Free;
  end;

  if AHit > 0 then
    Say2('Handwritten text was found in ' + IntToStrW(AHit) + ' of the selected images', 'The text for each image is now stored as the Description for that image.');
  else
    Say('No text found.');
end;