back to the blog

Generate Feature Images for Blog with Stable Diffusion API

Written on . Posted in Stable Diffusion API.
Generate Feature Images for Blog with Stable Diffusion API

In today's fast-paced digital world, attention spans are shorter than ever. With countless blogs, articles, and social media posts vying for our attention, it's essential to grab the reader's attention right away. One way to do this is by using feature images - compelling visuals that accompany the text and capture the reader's interest.

 

However, creating feature images within a short time that are both attention-grabbing and relevant to the content can be challenging. We can use AI tools like stable diffusion. Stable diffusion is a powerful image generation model that creates high-quality images with rich details and textures. Also feature images need some text on them describing what your blog is about. But Stable Diffusion can’t produce images with your desired text on them.

 

In this blog, we will see how to build an app that can make feature images for your blog that have your desired text on them. We will use Gradio, Pillow, and Stable Diffusion API to build this app. 

 

Building the application with Stable Diffusion and Pillow

Stable Diffusion as of now can’t generate images that contain the exact text we want it to contain. So we will create a workflow that can create an image with text in it.

The application that we are going to build will use the above workflow to generate images. We will use the Stable Diffusion API to generate images. We will build the interface with Gradio. For more about building interfaces with Gradio, you can refer to the Gradio documentation.

 

First, we import all the necessary libraries as shown below

import gradio as gr
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import requests
from io import BytesIO
import json

Now we give a title and description for our gradio app as shown below

title ="""<h2><center>Generate Images with Stable Diffusion API that contain your desired text</center></h2>"""
description ="""#### This app generates images that have your desired text in the generated images. Get your API key by signing up here [Stable Diffusion API](https://stablediffusionapi.com). This is a demo that shows the basic workflow to generate images with desired text in the images using Stable Diffusion and PIL"""

Gradio needs a function that takes all the inputs and outputs the desired result. In our app, we need a function that takes the API key, prompt text, text on the image, number of inference steps, and safety filter as inputs. We define such a function as follows

def get_image(key,prompt,image_text,inference_steps,filter):
  url = "https://stablediffusionapi.com/api/v3/dreambooth"
  payload = {"key":key,"prompt":prompt,"model_id":"midjourney","negative_prompt":"((out of frame)),((extra fingers)),mutated hands,((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), (((tiling))), ((naked)), ((tile)), ((fleshpile)), ((ugly)), (((abstract))), blurry, ((bad anatomy)), ((bad proportions)), ((extra limbs)), cloned face, (((skinny))), glitchy, ((extra breasts)), ((double torso)), ((extra arms)), ((extra hands)), ((mangled fingers)), ((missing breasts)), (missing lips),((ugly face)), ((fat)), ((extra legs)), anime","width":"1024","height":"768","samples":"1",
"num_inference_steps":inference_steps,"safety_checker":filter,"guidance_scale":7.5}
  headers = {}
  response = requests.request("POST",url,headers=headers,data=payload)
  url1 = str(json.loads(response.text)['output'][0])
  r = requests.get(url1)
  i = Image.open(BytesIO(r.content))
  i1 = ImageDraw.Draw(i)
  myFont = ImageFont.truetype(r'/content/Bestigia-Regular.ttf', size=80)
  i1.text((50,50),image_text,fill =(255,255,255),font=myFont)
  return i

We have used the stable diffusion dreambooth API endpoint to generate images. To find out more about all the available endpoints and different input parameters refer to the documentation. The function we defined sends a response request to the endpoint and fetches an image. The image is then processed through the pillow library to draw the text on the image. We need to download a font file and store it in the same directory as the app file. Finally, the image is returned by the function.

 

To display the interface of our app we use the Interface() class of gradio. We give our function, a textbox for the API key, a textbox for prompt input, a textbox for text on the image, a number box for the number of steps, and a checkbox for the safety filter as inputs. Our output is just an image so give the image as output. Finally, we launch the app using the .launch() class with the parameter debug as True. This is to know the error if any error occurs. The final code for the entire app looks like this below

import gradio as gr
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import requests
from io import BytesIO
import json

title = """<h2><center>Generate Images with Stable Diffusion API that contain your desired text</center></h2>"""
description = """#### This app generates images that has your desired text in the generated images. Get your API key by signing up here [Stable Diffusion API](https://stablediffusionapi.com). This is a demo that shows the basic workflow to generate images with desired text in the images using Stable Diffusion and PIL"""

def get_image(key, prompt, image_text, inference_steps, filter):
  url = "https://stablediffusionapi.com/api/v3/dreambooth"
  payload = {
    "key": key,
    "prompt": prompt,
    "model_id":"midjourney",
    "negative_prompt": "((out of frame)), ((extra fingers)), mutated hands, ((poorly drawn hands)), ((poorly drawn face)), (((mutation))), (((deformed))), (((tiling))), ((naked)), ((tile)), ((fleshpile)), ((ugly)), (((abstract))), blurry, ((bad anatomy)), ((bad proportions)), ((extra limbs)), cloned face, (((skinny))), glitchy, ((extra breasts)), ((double torso)), ((extra arms)), ((extra hands)), ((mangled fingers)), ((missing breasts)), (missing lips), ((ugly face)), ((fat)), ((extra legs)), anime",
    "width": "1024",
    "height": "768",
    "samples": "1",
    "num_inference_steps": inference_steps,"safety_checker": filter, "guidance_scale": 7.5}
  headers = {}
  response = requests.request("POST", url, headers=headers, data=payload)
  url1 = str(json.loads(response.text)['output'][0])
  r = requests.get(url1)
  i = Image.open(BytesIO(r.content))
  i1 = ImageDraw.Draw(i)
  myFont = ImageFont.truetype(r'/content/Bestigia-Regular.ttf', size=80)
  i1.text((50, 50), image_text, fill =(255, 255,255), font=myFont)
  return i

demo = gr.Interface(fn=get_image,
                    inputs = [gr.Textbox(label="Enter API key"), gr.Textbox(label="Enter the Prompt"), gr.Textbox(label='Enter the text you want on Image'), gr.Number(label="Enter number of steps"),gr.Checkbox(label="Safety filter")],
                    outputs = gr.Image(type='pil'), title = title, description = description).launch(debug='True')

Once you run the above code in your jupyter notebook you should be able to see the app like below. You can also host the app on hugging face spaces. You can play with this app using your API key. You can get the API key by signing up on the official website of Stable Diffusion API. The final app is below. Give all the inputs and try running the app to generate the images. 

There is always room for improvement. Try changing this app with different input parameters and different text styles and text fonts. The featured image for this blog is generated using the above app!!

 

If you're looking to generate images from text using the Dreambooth API, you may also want to consider our guide on "Generate Feature Images for Blog with Stable Diffusion API". In addition to providing a platform for generating custom feature images, Stable Diffusion API can be used in combination with Dreambooth API to create stunning visuals for your blog. By leveraging the strengths of both tools, you can quickly and easily generate high-quality images that accurately represent your content. 

 

Conclusion

In conclusion, generating feature images for blogs using stable diffusion is a game-changer for content creators looking to capture their readers' attention. This cutting-edge technology allows for the creation of high-quality images with rich details and textures that are both attention-grabbing and relevant to the content.

 

By following the step-by-step instructions and tips provided in this blog, you can use stable diffusion API to generate stunning feature images for your blog posts, making them more engaging and appealing to your audience.