Category: Deep Art API
Section: Code examples
Contents
- Prerequisites
- Install dependencies
- Example script (Python)
- Run the script
- Notes & troubleshooting
- See also
Prerequisites
- Python 3.9+
- A test image file (e.g.,
sample.jpg). - If an endpoint in your environment requires credentials, have your API key ready (see Get your API credentials).
Install dependencies
pip install requests
Example script (Python)
Save as deeparteffects.py in the same folder as sample.jpg.
# deeparteffects.py
# Usage: python deeparteffects.py
# Set an env var API_KEY if any endpoint in your setup requires it.
import base64
import os
import time
import requests
BASE = "https://api.deeparteffects.com/v1"
HEADERS = {
"Content-Type": "application/json",
# If your environment requires auth for a call, uncomment the next line:
# "x-api-key": os.getenv("API_KEY", "")
}
def list_styles():
r = requests.get(f"{BASE}/noauth/styles")
r.raise_for_status()
return r.json()
def upload_image(style_id: str, image_path: str, image_size: int = 1024):
with open(image_path, "rb") as f:
b64 = base64.b64encode(f.read()).decode("ascii")
payload = {
"styleId": style_id,
"imageBase64Encoded": b64,
"imageSize": image_size,
}
r = requests.post(f"{BASE}/noauth/upload", headers=HEADERS, json=payload)
r.raise_for_status()
return r.json()["submissionId"]
def check_result(submission_id: str):
r = requests.get(f"{BASE}/noauth/result", params={"submissionId": submission_id})
r.raise_for_status()
return r.json() # { status, url? }
def main():
print("Listing styles…")
styles = list_styles()
if not styles:
raise SystemExit("No styles returned")
style = styles[0]
print("Using style:", style)
print("Uploading image…")
submission_id = upload_image(style["id"], "sample.jpg", 1024)
print("Submission ID:", submission_id)
print("Polling result…")
attempt = 0
while True:
attempt += 1
res = check_result(submission_id)
status = res.get("status")
if status == "finished":
url = res.get("url")
print("Finished. Result URL:", url)
# Download the result
img = requests.get(url)
img.raise_for_status()
with open("result.png", "wb") as f:
f.write(img.content)
print("Saved to result.png")
break
if status == "error":
raise SystemExit("Processing error")
time.sleep(min(1 * attempt, 5)) # simple backoff 1s → 5s
if __name__ == "__main__":
main()
Run the script
python deeparteffects.py
You should see logs for Listing styles, Uploading image, Polling result, and a file result.png created on success.
Notes & troubleshooting
-
401/403 — Add your API key header if the endpoint you’re calling requires it; confirm the exact path (
/noauth/vs authenticated). - 429 — Increase the backoff between polls or reduce concurrency.
- Invalid base64 — Ensure you read the file as binary and base64‑encode it once.
-
Large inputs — Try a smaller
imageSizefirst to validate your flow, then increase.
Comments
0 comments
Please sign in to leave a comment.