Python Examples

The Image Toolkit Python SDK covers image processing, generation, metadata extraction, favicon discovery, and Liquid-template rendering.

Install

pip install toolkitapi

Basic setup

from toolkitapi import Image

image = Image(api_key="tk_...")

REST API Examples

Resize an image

curl -X POST "https://image.toolkitapi.io/v1/image/resize" \
  -H "X-API-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://toolkitapi.io/photo.jpg", "width": 800, "maintain_aspect": true, "format": "jpeg"}'
const resp = await fetch("https://image.toolkitapi.io/v1/image/resize", {
  method: "POST",
  headers: { "X-API-Key": "YOUR_KEY", "Content-Type": "application/json" },
  body: JSON.stringify({ url: "https://toolkitapi.io/photo.jpg", width: 800, format: "jpeg" }),
});
const data = await resp.json();
console.log(`Resized: ${data.width}x${data.height}`);

Convert format

curl -X POST "https://image.toolkitapi.io/v1/image/convert" \
  -H "X-API-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://toolkitapi.io/photo.jpg", "format": "webp", "quality": 85}'

Generate from template

curl -X POST "https://image.toolkitapi.io/v1/image/from-template" \
  -H "X-API-Key: YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"template": "<html><body><h1>{{ title }}</h1></body></html>", "variables": {"title": "Hello"}, "width": 1200, "height": 630, "format": "png"}'

Python SDK Quick Reference

Resize an image

with Image(api_key="tk_...") as image:
    result = image.resize(
        url="https://toolkitapi.io/photo.jpg",
        width=800,
        maintain_aspect=True,
        format="webp",
    )
    print(result["new_size"])

Convert between formats

with Image(api_key="tk_...") as image:
    result = image.convert(
        "png",
        "webp",
        url="https://toolkitapi.io/logo.png",
        quality=85,
    )
    print(result["content_type"])

Generate a QR code

with Image(api_key="tk_...") as image:
    result = image.qr("https://toolkitapi.io", format="png")
    print(result["format"])

Extract colors

with Image(api_key="tk_...") as image:
    colors = image.extract_colors(url="https://toolkitapi.io/photo.jpg", count=5)
    print(colors)

Render a Liquid template as an image

with Image(api_key="tk_...") as image:
    card = image.from_template(
        template="<html><body><h1>{{ title }}</h1></body></html>",
        variables={"title": "Launch day"},
        width=1200,
        height=630,
    )
    print(card["width"], card["height"])