Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.4.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oBdImageData
LOCAL oAi
LOCAL oAskParams

nSuccess := 0

//  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.
oBdImageData := CreateObject("Chilkat.BinData")
nSuccess := oBdImageData:LoadFile("qa_data/jpg/kid_blue_coat.jpg")
IF (nSuccess == 0)
    ? oBdImageData:LastErrorText
    oBdImageData:destroy()
    RETURN
ENDIF

oAi := CreateObject("Chilkat.Ai")

oAi:Provider := "openai"

//  Use your provider's API key.
oAi:ApiKey := "MY_API_KEY"

//  Choose a model.
oAi:Model := "gpt-5.6-terra"

oAskParams := CreateObject("Chilkat.JsonObject")
oAskParams:UpdateString("image.output_format", "jpeg")
oAi:SetAskParams(oAskParams)

oAi:InputAddImageData(oBdImageData, "")
oAi:InputAddText("Modify the image by replacing the blue coat with a Metallica T-shirt.")

//  Give the AI some time (2 minutes).
oAi:IdleTimeoutMs := 120000

//  Ask the AI for image output.
nSuccess := oAi:Ask("image")
IF (nSuccess == 0)
    ? oAi:LastErrorText
    oBdImageData:destroy()
    oAi:destroy()
    oAskParams:destroy()
    RETURN
ENDIF

//  Get the image response data.;
nSuccess := oAi:GetOutputBd(oBdImageData)
IF (nSuccess == 0)
    ? oAi:LastErrorText
    oBdImageData:destroy()
    oAi:destroy()
    oAskParams:destroy()
    RETURN
ENDIF

oBdImageData:WriteFile("c:/aaworkarea/out.jpg")

? "Success."

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

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

oBdImageData:destroy()
oAi:destroy()
oAskParams:destroy()