Sample code for 30+ languages & platforms
DataFlex

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Variant vBdImageData
    Handle hoBdImageData
    Handle hoAi
    Variant vAskParams
    Handle hoAskParams
    String sTemp1

    Move False To iSuccess

    // 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.
    Get Create (RefClass(cComChilkatBinData)) To hoBdImageData
    If (Not(IsComObjectCreated(hoBdImageData))) Begin
        Send CreateComObject of hoBdImageData
    End
    Get ComLoadFile Of hoBdImageData "qa_data/jpg/kid_blue_coat.jpg" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoBdImageData To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatAi)) To hoAi
    If (Not(IsComObjectCreated(hoAi))) Begin
        Send CreateComObject of hoAi
    End

    Set ComProvider Of hoAi To "openai"

    // Use your provider's API key.
    Set ComApiKey Of hoAi To "MY_API_KEY"

    // Choose a model.
    Set ComModel Of hoAi To "gpt-5"

    Get Create (RefClass(cComChilkatJsonObject)) To hoAskParams
    If (Not(IsComObjectCreated(hoAskParams))) Begin
        Send CreateComObject of hoAskParams
    End
    Get ComUpdateString Of hoAskParams "image.output_format" "jpeg" To iSuccess
    Get pvComObject of hoAskParams to vAskParams
    Get ComSetAskParams Of hoAi vAskParams To iSuccess

    Get pvComObject of hoBdImageData to vBdImageData
    Get ComInputAddImageData Of hoAi vBdImageData "" To iSuccess
    Get ComInputAddText Of hoAi "Modify the image by replacing the blue coat with a Metallica T-shirt." To iSuccess

    // Give the AI some time (2 minutes).
    Set ComIdleTimeoutMs Of hoAi To 120000

    // Ask the AI for image output.
    Get ComAsk Of hoAi "image" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Get the image response data.;
    Get pvComObject of hoBdImageData to vBdImageData
    Get ComGetOutputBd Of hoAi vBdImageData To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoAi To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComWriteFile Of hoBdImageData "c:/aaworkarea/out.jpg" To iSuccess

    Showln "Success."

    // -------------------------------
    // Sample Input:
    // -------------------------------
    // image

    // -------------------------------
    // Sample Output:
    // -------------------------------
    // image


End_Procedure