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.
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.
You'll need to install the required libraries. Open a terminal window and then enter the following instructions:
pip install requests
This code will:
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)
© 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!
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
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.
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.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!
In this entertaining techno-thriller for adults, Sean McManus takes a slice through the music industry: from the boardroom to the stage; from the studio to the record fair.