Image Generation Tutorial Using Python And The Leaf API


Hello ๐Ÿ˜Ž In this tutorial I will show you how you can generate images using Python and the Leaf API.


  • A basic understanding of Python
  • A basic understanding of RESTful APIs
  • A Leaf API account (

With all three you should be able to follow the tutorial with ease. ๐Ÿ˜ƒ

Creating The Virtual Environment

First we need to create the virtual environment for our project, to do this you need to run the following command:

python3 -m venv env

Then you need to activate the virtual environment:

source env/bin/activate

Installing The Dependencies

Next we need to install the dependencies required for this project, open up a file called "requirements.txt" and add the following:


We will use opencv to show and save the outputted image and requests to send requests to the Leaf API.

You can use the following command to install the dependencies:

pip install -r requirements.txt

All done now we can actually start coding the project. ๐Ÿ˜†

Coding The Project

Now that's all that is needed is to actually code the project. Open a file called "" and add the following imports:

import requests
import json
import time
import cv2
import numpy as np
import argparse

We also need to define some constants the first one being your Leaf API key:

API_KEY = "your_api_key_here"

Next we need to defined the headers used with the request:

    "accept": "application/json",
    "content-type": "application/json",
    "authorization": f"Bearer {API_KEY}"

Finally we need to provide some sample images for our model, I have decided to use random pictures of Obama but feel free to provide your own.


Now that we have the imports and constants sorted we can now code the various functions that will be used in this project:

def create_model(title):
    url = ""

    payload = { 
        "title": title,
        "subjectKeyword": "@me"

    response =, json=payload, headers=HEADERS)

    model_id = json.loads(response.text)["id"]
    return model_id

create_model creates a new image generation model with the provided title.

def upload_image_samples(model_id):
    url = f"{model_id}/samples/url"

    payload = {"images": IMAGES}
    response =, json=payload, headers=HEADERS)

upload_image_samples uploads the sample images to the created model.

def queue_training_job(model_id):
    url = f"{model_id}/queue"
    response =, headers=HEADERS)
    data = json.loads(response.text)


    version_id = data["id"]
    status = data["status"]

    print(f"Version ID: {version_id}. Status: {status}")

    return version_id, status

queue_training_job queues the training job for the model.

def get_model_version(model_id, version_id):
    url = f"{model_id}/versions/{version_id}"
    response = requests.get(url, headers=HEADERS)
    data = json.loads(response.text)

    version_id = data["id"]
    status = data["status"]

    print(f"Version ID: {version_id}. Status: {status}")

    return version_id, status

get_model_version retrieves the model's version and status.

def generate_image(model_id, prompt):
    url = f"{model_id}/inferences"

    payload = {
        "prompt": prompt,
        "steps": 50,
        "width": 512,
        "height": 512,
        "numberOfImages": 1,
        "seed": 4523184

    response =, json=payload, headers=HEADERS)
    data = json.loads(response.text)

    inference_id = data["id"]
    status = data["status"]

    print(f"Inference ID: {inference_id}. Status: {status}")

    return inference_id, status

generate_image generates a new image based on the provided prompt which the user provides when running this script. Feel free to play around with the variables.

def get_inference_job(model_id, inference_id):
    url = f"{model_id}/inferences/{inference_id}"

    response = requests.get(url, headers=HEADERS)
    data = json.loads(response.text)

    inference_id = data["id"]
    state = data["state"]
    image = None

    if len(data["images"]):
        image = data["images"][0]["uri"]

    print(f"Inference ID: {inference_id}. State: {state}")

    return inference_id, state, image

get_inference_job retrieves the inference job's state and generated image/s.

def save_and_show_image(image_url, image_name="output.jpg"):
    arr = np.asarray(bytearray(requests.get(image_url).content), dtype=np.uint8)
    img = cv2.imdecode(arr, -1)

    cv2.imwrite(image_name, img)
    cv2.imshow("Generated Image", img)

save_and_show_image saves the generated image and then displays it to the user.

Phew! That's a lot of functions. ๐Ÿ˜…

Now we can finish it off by defining a main function:

def main(prompt):
    model_id = create_model("Sample")


    version_id, status = queue_training_job(model_id)
    while status != "finished":
        version_id, status = get_model_version(model_id, version_id)

    inference_id, status = generate_image(
        prompt = prompt
    while status != "finished":
        inference_id, status, image = get_inference_job(model_id, inference_id)


if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument("-p", "--prompt", required = True, help = "Prompt for image generation")
    args = vars(ap.parse_args())


The main function parses the command line arguments and then starts the image generating process. The process takes some time to finish so we wait until the status of the image generation is turned to finish.

Finally we can run the script via the following command:

python main -p "A photo of @me with a black hat and sunglasses"

Feel free to grab a coffee as this does take some time, but if it goes well you should see something like the following:

Generated Image

Wow! ๐Ÿ˜ฏ You will need to upgrade your Leaf account if you want to generate more images.


Here I have shown how to use Python and Leaf API to generate images. I was very impressed with the results and it was a lot of fun trying out this API.

If you know any similar APIs etc. Please let me know in the comments. ๐Ÿ˜ƒ

Happy Coding!

As always you can find the sample code on my Github:

Like me work? I post about a variety of topics, if you would like to see more please like and follow me. Also I love coffee.

โ€œBuy Me A Coffeeโ€

If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the following course