Sample code for 30+ languages & platforms
PureBasic

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkAi.pb"
IncludeFile "CkBinData.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.

    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")
    CkJsonObject::ckUpdateString(askParams,"image.size","1024x1024")
    CkJsonObject::ckUpdateString(askParams,"image.quality","low")
    CkAi::ckSetAskParams(ai,askParams)

    CkAi::ckInputAddText(ai,"Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf")

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

    ; Get the image response data.
    bdImageData.i = CkBinData::ckCreate()
    If bdImageData.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkAi::ckGetOutputBd(ai,bdImageData)
    If success = 0
        Debug CkAi::ckLastErrorText(ai)
        CkAi::ckDispose(ai)
        CkJsonObject::ckDispose(askParams)
        CkBinData::ckDispose(bdImageData)
        ProcedureReturn
    EndIf

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

    Debug "Success."


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


    ProcedureReturn
EndProcedure