DEV Community

Cover image for Image Generation Tutorial Using Python And The Leaf API
Ethan
Ethan

Posted on • Originally published at ethan-dev.com

Image Generation Tutorial Using Python And The Leaf API

Introduction

Hello 😎 In this tutorial I will show you how you can generate images using Python and the Leaf API.


Requirements

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
Enter fullscreen mode Exit fullscreen mode

Then you need to activate the virtual environment:

source env/bin/activate
Enter fullscreen mode Exit fullscreen mode

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:

opencv-python
requests
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 "main.py" and add the following imports:

import requests
import json
import time
import cv2
import numpy as np
import argparse
Enter fullscreen mode Exit fullscreen mode

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

API_KEY = "your_api_key_here"
Enter fullscreen mode Exit fullscreen mode

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

HEADERS = { 
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": f"Bearer {API_KEY}"
}
Enter fullscreen mode Exit fullscreen mode

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.

IMAGES = [ 
    "https://i.natgeofe.com/k/271050d8-1821-49b8-bf0b-3a4a72b6384a/obama-portrait__3x2.jpg",
    "https://d3hjzzsa8cr26l.cloudfront.net/516e6836-d278-11ea-a709-979a0378f022.jpg",
    "https://hips.hearstapps.com/hmg-prod/images/gettyimages-1239961811.jpg"
]
Enter fullscreen mode Exit fullscreen mode

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 = "https://api.tryleap.ai/api/v1/images/models"

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

    response = requests.post(url, json=payload, headers=HEADERS)

    model_id = json.loads(response.text)["id"]
    return model_id
Enter fullscreen mode Exit fullscreen mode

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

def upload_image_samples(model_id):
    url = f"https://api.tryleap.ai/api/v1/images/models/{model_id}/samples/url"

    payload = {"images": IMAGES}
    response = requests.post(url, json=payload, headers=HEADERS)
Enter fullscreen mode Exit fullscreen mode

upload_image_samples uploads the sample images to the created model.

def queue_training_job(model_id):
    url = f"https://api.tryleap.ai/api/v1/images/models/{model_id}/queue"
    response = requests.post(url, headers=HEADERS)
    data = json.loads(response.text)

    print(response.text)

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

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

    return version_id, status
Enter fullscreen mode Exit fullscreen mode

queue_training_job queues the training job for the model.

def get_model_version(model_id, version_id):
    url = f"https://api.tryleap.ai/api/v1/images/models/{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
Enter fullscreen mode Exit fullscreen mode

get_model_version retrieves the model's version and status.

def generate_image(model_id, prompt):
    url = f"https://api.tryleap.ai/api/v1/images/models/{model_id}/inferences"

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

    response = requests.post(url, 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
Enter fullscreen mode Exit fullscreen mode

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"https://api.tryleap.ai/api/v1/images/models/{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
Enter fullscreen mode Exit fullscreen mode

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)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

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")

    upload_image_samples(model_id)

    version_id, status = queue_training_job(model_id)
    while status != "finished":
        time.sleep(10)
        version_id, status = get_model_version(model_id, version_id)

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

    save_and_show_image(image)

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

    main(args["prompt"])
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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.


Conclusion

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:
https://github.com/ethand91/python-leaf-image-generation


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

Top comments (0)