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
.
Parameter | Type | Default | Description |
---|---|---|---|
params | Dictionary | None | Calling 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
Parameter | Type | Default | Description |
---|---|---|---|
path | String | None | The file path of an image or mask. |
.remoteImg2Base64(url)
Get an image or mask remotely and encode it in Base64 format. Return value: String
Parameter | Type | Default | Description |
---|---|---|---|
url | String | None | The 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
.
Parameter | Type | Default | Description |
---|---|---|---|
taskUID | String | None | The task ID was obtained after calling the Generate and Modify class APIs. |
interval | Int | 3000 | Optional. Polling interval in milliseconds. |
callback | Function | null | Optional. 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
Parameter | Type | Default | Description |
---|---|---|---|
publicKey | String | None | Public key, available on the User Center page. |
privateKey | String | None | Private key, available on the User Center page. |
gateway | String | https://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)