Image Generation Tutorial Using Python And The Leaf API
Tags: python, beginners, tutorial, ai
Published: 2023-03-21 03:35:53

## Introduction

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

---

## Requirements

- A basic understanding of Python
- A basic understanding of RESTful APIs
- A Leaf API account (https://www.tryleap.ai/login)

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:

```bash
python3 -m venv env
```

Then you need to activate the virtual environment:

```bash
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:

```text
opencv-python
requests
```

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:

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

```python
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:

```python
API_KEY = "your_api_key_here"
```

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

```python
HEADERS = { 
    "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.

```python
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"
]
```

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

```python
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
```

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

```python
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)
```

upload_image_samples uploads the sample images to the created model.

```python
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
```

queue_training_job queues the training job for the model.

```python
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
```

get_model_version retrieves the model's version and status.

```python
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
```

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.

```python
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
```

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

```python
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()
```

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:

```python
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"])
```

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:

```bash
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](https://i.ibb.co/F4DKKQ7/output.jpg)

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”](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/ethand9999)

If you are looking to learn Algorithm Patterns to ace the coding interview I recommend the [following course](https://algolab.so/p/algorithms-and-data-structure-video-course?affcode=1413380_bzrepgch)