Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkAi.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 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.
    bdImageData.i = CkBinData::ckCreate()
    If bdImageData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkBinData::ckLoadFile(bdImageData,"qa_data/jpg/kid_blue_coat.jpg")
    If success = 0
        Debug CkBinData::ckLastErrorText(bdImageData)
        CkBinData::ckDispose(bdImageData)
        ProcedureReturn
    EndIf

    ai.i = CkAi::ckCreate()
    If ai.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkAi::setCkProvider(ai, "openai")

    ; Use your provider's API key.
    CkAi::setCkApiKey(ai, "MY_API_KEY")

    ; Choose a model.
    CkAi::setCkModel(ai, "gpt-5")

    askParams.i = CkJsonObject::ckCreate()
    If askParams.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(askParams,"image.output_format","jpeg")
    CkAi::ckSetAskParams(ai,askParams)

    CkAi::ckInputAddImageData(ai,bdImageData,"")
    CkAi::ckInputAddText(ai,"Modify the image by replacing the blue coat with a Metallica T-shirt.")

    ; Give the AI some time (2 minutes).
    CkAi::setCkIdleTimeoutMs(ai, 120000)

    ; Ask the AI for image output.
    success = CkAi::ckAsk(ai,"image")
    If success = 0
        Debug CkAi::ckLastErrorText(ai)
        CkBinData::ckDispose(bdImageData)
        CkAi::ckDispose(ai)
        CkJsonObject::ckDispose(askParams)
        ProcedureReturn
    EndIf

    ; Get the image response data.;
    success = CkAi::ckGetOutputBd(ai,bdImageData)
    If success = 0
        Debug CkAi::ckLastErrorText(ai)
        CkBinData::ckDispose(bdImageData)
        CkAi::ckDispose(ai)
        CkJsonObject::ckDispose(askParams)
        ProcedureReturn
    EndIf

    CkBinData::ckWriteFile(bdImageData,"c:/aaworkarea/out.jpg")

    Debug "Success."

    ; -------------------------------
    ; Sample Input:
    ; -------------------------------
    ; image

    ; -------------------------------
    ; Sample Output:
    ; -------------------------------
    ; image


    CkBinData::ckDispose(bdImageData)
    CkAi::ckDispose(ai)
    CkJsonObject::ckDispose(askParams)


    ProcedureReturn
EndProcedure