back to the blog

How to generate images from text using the stable diffusion API?

Written on . Posted in Stable Diffusion API.
How to generate images from text using the stable diffusion API?

Stable Diffusion API is a cutting-edge company providing API as a service for image generation. With the help of advanced algorithms and machine learning models, Stable Diffusion API enables users to generate high-quality images from text inputs with ease. The API offers a range of models that can be used to generate images, making it suitable for a variety of applications, including visual content creation, and more.

 

The Stable Diffusion technology incorporated into the API helps to ensure that the images generated are not only visually appealing but also accurate and consistent. This makes the API an ideal solution for businesses and individuals who want to create engaging visual content quickly and easily. 

 

In this tutorial, we will see the usage of the text2img endpoint of Stable Diffusion API, with an example. We'll cover everything you need to know about using the Text-to-Image API and Stable Diffusion to generate images from text. So, whether you're a beginner or an experienced user, this tutorial will help you get up and running quickly and easily.

 

Getting started with the Text-to-Image API

Setting up an Account

To use the API you need to have an account. Sign up here on the official website. You can buy a subscription. Find all the plans of the API on the pricing page. Once you signup and get an account you will get access to the API on your dashboard. It looks something like below.

You can click ‘View’ to view your API. Once you get the API key you can start using it to generate images by making the API calls. In the below section, we will see how to make an API call in python.

Using API calls

We can use either the requests module or the http.client method to send an API call and generate images. Below is the code to use the requests module:

import requests

url = "https://stablediffusionapi.com/api/v3/text2img"
payload = {
 "key": "Your API key",
 "prompt": "a man with a hat standing on hill",
 "width": "512",
 "height": "512",
 "samples": "1",
 "num_inference_steps": "20",
 "guidance_scale": 7.5,
"safety_checker":"yes"
}

headers = {}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

In the above code, first, we imported the requests module. We used the API link for the text2img endpoint to send the request. Each endpoint has its URL. So we need to use the appropriate URL for specific endpoints. In the next step, we created the payload which is basically the parameters that our endpoint utilizes to generate the image from the text. After that, we created a response object that interacts with the API using all the parameters that we defined above. Finally, we print the response object using the response.text method. The output of the above code looks something like the one below. 

Output:

The output is a JSON object. We can see a parameter called ‘output’ which contains the link to our generated image. Also, observe that there is another parameter ‘generationTime’ which shows the time to generate the images. In this case, it took less than two minutes to generate the image. This is how powerful our API is. You can explore subscriptions to get faster generations as per your need. Our generated image is as follows:

Thus in a simple way, we can generate an image with stable diffusion API using the requests module.

Exploring the parameters for the Text-to-Image Endpoint

We can pass several parameters to generate and customize the generated images. The following are the parameters that are available as input in order to customize the image generation process:

 

key: Your API key
prompt: The sentence that is used to generate the image
samples: The number of images that you want to generate
negative_prompt: The things that you want to exclude from the generated image.
width: The width of the image. Maximum size is 1024x768 or 768x1024.
height: The height of the image. Maximum size is 1024x768 or 768x1024
prompt_strength: The strength of your prompt. This is used when using init_image.
num_inference_steps: The number of denoising steps(minimum:1, maximum:50
guidance_scale: This is a scale for classifier-free guidance(minimum:1, maximum:20)
enhance_prompt: This enhances your prompts for better results. The default is Yes. 
seed: This is a random seed for your prompt. Leave it blank to give a random seed.
webhook: This is a webhook to call when image generation is completed.
track_id: This is a tracking id to track your API call

 

We can use the above parameters for image generation of custom size and we can also generate multiple images at the same time. 

Advantages of using the Stable Diffusion API in text-to-image conversions

There are several advantages to using Stable Diffusion API in Text-to-Image conversions.

  • Firstly, it helps to ensure that the images generated are of high quality and consistent. 
  • Secondly, it makes the image generation process more efficient, as the API can generate images much faster and with greater accuracy. 
  • Finally, it reduces the need for manual intervention, allowing you to focus on creating engaging visual content rather than worrying about the technical details of image generation.

Conclusion

In conclusion, the Stable Diffusion APIs text2img endpoint is a powerful tool that enables you to generate high-quality images from text input. The Stable Diffusion technology incorporated into the API helps to ensure that the images generated are not only visually appealing but also accurate and consistent, making them perfect for use in visual content.

 

In this tutorial, we've covered everything you need to know about using the Stable Diffusion API's text2img endpoint to generate images from text. Whether you're a beginner or an experienced user, this tutorial should help you get up and running quickly and easily. So go ahead and start exploring the world of text-to-image conversions today by signing up here.