SDKs
Python

Python SDK

This SDK is aimed at users who code in Python, auto-signing and simplifying the calling process. Download the following file and save it as api.py:

Functions

.call(params)

Calls the API. Return value: Dictionary.

ParameterTypeDefaultDescription
paramsDictionaryNoneCalling parameters, consult the API documentation for more information. The parameters PublicKey, PrivateKey and Signature are not required.

.localImg2Base64(path)

Get an image or mask from a local disk and encode it in Base64 format. Return value: String

ParameterTypeDefaultDescription
pathStringNoneThe file path of an image or mask.

.remoteImg2Base64(url)

Get an image or mask remotely and encode it in Base64 format. Return value: String

ParameterTypeDefaultDescription
urlStringNoneThe URL address of an image or mask. If the link does not start with "https://", the SDK will try to automatically fix it.

.pollingTask(taskUID, interval, callback)

Poll a task. Return value: Dictionary.

ParameterTypeDefaultDescription
taskUIDStringNoneThe task ID was obtained after calling the Generate and Modify class APIs.
intervalInt3000Optional. Polling interval in milliseconds.
callbackFunctionnullOptional. A callback function that returns an Dictionary containing detailed information about the current task.
If the interval isn't set, the callback can be used directly as the second argument.

.setKeys(publicKey, privateKey)

Set the public and private keys required to call the API. Return value: None

ParameterTypeDefaultDescription
publicKeyStringNonePublic key, available on the User Center page.
privateKeyStringNonePrivate key, available on the User Center page.
gatewayStringhttps://api.picpik.ai/ (opens in a new tab)Optional. The API gateway of the global site. If your account belongs to the site of Chinese mainland, the gateway should be https://api.picpikai.com/.
If the link does not start with "https://", the SDK will try to fix it automatically.

Demo

A demo of generating a bird and remove the background.

import api
 
# Set your API Keys. Keys can be found on the user account page after logging into the official website.
api.setKeys(
    publicKey = "Your Public Key",
    privateKey = "Your Private Key"
)
 
# Get the models' list. A main model and relevant parameters are required when generating images. Different from calling the APIs directly, the SDK will automatically provide common 
data = api.call({
    "Action": "ListModels"
})
model = data["MainModels"]["PICPIK General"]
 
# Generate an image by specifying a model from the models' list.
data = api.call({
    "Action": "GenerateImages",
    "MainModel": model,
    "PositivePrompts": ["One Bird"],
})
taskUID = data["TaskUID"]
 
# Polling the task by it's task UID.
data = api.pollingTask(taskUID, lambda taskDetail:print(taskDetail["Status"]))
 
# Get the URL of the generated image and encode it in Base64.
imageURL = data["ImgList"][0]
base64Img = api.remoteImg2Base64(imageURL)
 
# Remove the background
data = api.call({
    "Action": "RemoveBackground",
    "RawImage": base64Img
})
taskUID = data["TaskUID"]
 
# Polling the task by it's task UID.
data = api.pollingTask(taskUID, lambda taskDetail:print(taskDetail["Status"]))
 
# Get the URL of the generated image after finishing the task.
imageURL = "https://" + data["ImgList"][0]
print(imageURL)