Home > Articles > Sean's Raspberry Pi coding tutorials > DeepAI Text to image API

Use the DeepAI Text to Image API to generate images using Stable Diffusion

A photorealistic image of a giant balloon elephant, with two heads and part of it spilling out of the frame.DeepAI provides APIs that help you to use artificial intelligence (AI) models in your own Python programs. In this example, I'll show you how to generate an image using their text to image API, which currently points at Stable Diffusion, a popular open source image generation tool. The API gives you a web link where your image is stored, so my Python program also shows you how to download an image from a link.

To use the tool, you provide a text description of the image you'd like created for you. For the image on the right, I requested a photo of an elephant made out of balloons. This image is a great example of how Stable Diffusion can be used to create convincing images of something that never existed, but also shows some of the shortcomings.

You'll notice that this elephant has two heads, and spills out of the frame. To try to reduce these effects, you can use a negative prompt that describes things you don't want in your picture, such as too many heads and legs. It doesn't always work, but it provides a hint to the software that can be useful. You may need to run the program a few times to get an image you can use.

You will need to register for your own API key and set up billing. You can buy 100 API requests (or images) for $5.

Installing the dependencies

You'll need to install the required libraries. Open a terminal window and then enter the following instructions:

pip install requests

Get the code!

This code will:

  • Request an image from DeepAI
  • Download the image and save it, with the current date and time used as the filename

As a result, you can run it many times without having to change the code or enter new filenames. Because the program takes longer than a second to run, each time you run it, the image will be saved with a new name. If you modify the program to download multiple images without stopping, you should check this still works correctly for you.

The image prompt is coded into the program (an elephant playing football). I've also included a negative prompt which provides hints to help improve image quality. You could ask the user to input the prompt, or take it from a list or other data source.

## Generate and download a Stable Diffusion image using DeepAI API
## by Sean McManus - www.sean.co.uk

import os, requests, datetime

date_time_str = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
filename = f"hello world {date_time_str}.jpg"
directory = "output"

try:
    os.mkdir(directory)
except:
    # directory already exists
    pass

r = requests.post(
"https://api.deepai.org/api/text2img",
data={
    'text' : "An elephant playing football", 
    'negative_prompt': "duplicate, out of frame, poorly drawn face, mutation, \
    deformed, blurry, extra limbs, bad anatomy, gross proportions, \
    malformed limbs, missing legs, extra legs, extra trunks, extra ears",
      'width': "768", 'height': "768",
      'grid_size': "1"},
headers={'api-key': 'YOUR API KEY'} ### ADD YOUR API KEY HERE
)

image_url = (r.json()["output_url"])
filepath = os.path.join(directory, filename)
img_data = requests.get(image_url).content
with open(filepath, 'wb') as handler:
    handler.write(img_data)

More Raspberry Pi projects

Find more Raspberry Pi projects and tutorials here.

Credits

© Sean McManus. All rights reserved.

Visit www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Coding Compendium

Coding Compendium

A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.

Web Design in Easy Steps

Web Design IES

Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.

100 Top Tips: Microsoft Excel

100 Top Tips: Microsoft Excel

Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.

Scratch Programming in Easy Steps

Scratch Programming IES

This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Newsletter | Blog | Copywriting Services | Books | Free book chapters | Articles | Music | Photos | Games | Shop | About