⚡️ Saturday AI Sparks 🤖 - 🎨🤖 Generate Images with Stable Diffusion


Description:

Text-to-Image generation has become one of the most exciting applications of AI. With just a text prompt, you can create detailed, high-quality visuals for design, art, or creative projects.

In this post, we’ll use the Stable Diffusion API from Stability AI to generate images directly from Python.


Why Stable Diffusion?

  • Creative control → the image is guided entirely by your prompt.
  • Flexible → choose resolution, steps, guidance scale, and samplers.
  • Accessible → available via REST API, no need for heavy local setup.
  • Use cases → digital art, marketing visuals, mockups, and prototyping.

Installing Requirements

We’ll use requests to interact with the API and Pillow/matplotlib for preview.

pip install requests pillow matplotlib


Setting Up the API Key

You’ll need a free account on Stability AI to generate an API key. Keep it private.

import os

STABILITY_API_KEY = os.getenv("STABILITY_API_KEY")

if not STABILITY_API_KEY or STABILITY_API_KEY == "YOUR_API_KEY_HERE":
    raise ValueError("Please set STABILITY_API_KEY to your actual key before running.")

Defining the Prompt

The quality of results depends heavily on your prompt. Here’s an example:

prompt = "A futuristic hi-tech coding desk with holographic screens, neon lighting, ultra-detailed, cinematic digital art"
negative_prompt = "blurry, low-res, distorted, text overlay, watermark"


Sending the API Request

Stable Diffusion requires JSON input with text_prompts and generation parameters.

import requests

endpoint = "<https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image>"

headers = {
    "Authorization": f"Bearer {STABILITY_API_KEY}",
    "Content-Type": "application/json",
    "Accept": "image/png",
}

payload = {
    "text_prompts": [
        {"text": prompt, "weight": 1},
        {"text": negative_prompt, "weight": -1}
    ],
    "cfg_scale": 7.5,
    "steps": 30,
    "width": 1024,
    "height": 1024,
    "samples": 1,
}

resp = requests.post(endpoint, headers=headers, json=payload)

if resp.status_code == 200:
    with open("sdxl_output.png", "wb") as f:
        f.write(resp.content)
    print("✅ Image saved as sdxl_output.png")
else:
    print("❌ Generation failed:", resp.text[:200])


Previewing the Result

You can display the output in a notebook or script:

from PIL import Image
import matplotlib.pyplot as plt

img = Image.open("sdxl_output.png")

plt.figure(figsize=(2, 2))  # passport-size preview
plt.imshow(img)
plt.axis("off")
plt.show()


Sample Output

With the example prompt above, the API generates an artistic hi-tech desk scene complete with neon lights and holographic screens. Results vary with each run.


Key Takeaways

  • The Stable Diffusion API makes image generation as simple as a POST request.
  • Prompt engineering is the key — detailed prompts produce better results.
  • You can customize style, resolution, and guidance for different aesthetics.
  • Useful for designers, creators, and developers experimenting with generative AI.

Code Snippet:

import os

# Option 1: set directly (not recommended for shared notebooks)
STABILITY_API_KEY = "YOUR_STABILITY_API_KEY_HERE"

# Option 2 (recommended): read from environment
# STABILITY_API_KEY = os.getenv("STABILITY_API_KEY")

if not STABILITY_API_KEY:
    raise ValueError("Please set STABILITY_API_KEY to your actual key before running.")

# Your creative prompt
prompt = "a cozy reading nook with plants, warm sunlight, and a cat sleeping on a chair, cinematic, ultra-detailed"

# Optional negative prompt to avoid unwanted elements
negative_prompt = "blurry, low-res, text, watermark, logo, distorted"

# Image settings (SDXL 1024 works best with 1024x1024)
width, height = 1024, 1024
steps = 30
cfg_scale = 7.5
sampler = "K_DPM_2_ANCESTRAL"  # or "K_EULER", "K_DPM_2", etc.
output_path = "sdxl_output.png"



import os, requests, json

STABILITY_API_KEY = "YOUR_STABILITY_API_KEY_HERE"
if not STABILITY_API_KEY:
    raise ValueError("Please set STABILITY_API_KEY to your actual key before running.")

prompt = (""
          "A futuristic hi-tech coding desk setup with multiple holographic monitors, "
          "glowing neon keyboard, sleek laptop, and ambient blue-purple lighting. "
          "The desk is minimalistic, surrounded by floating code screens and cyberpunk "
          "vibes, ultra-detailed, cinematic lighting, 8k digital art.")

negative_prompt = "blurry, low-res, text, watermark, logo, distorted"

width, height = 1024, 1024      # SDXL likes 1024x1024
steps = 30
cfg_scale = 7.5
sampler = "K_DPM_2_ANCESTRAL"   # e.g., "K_EULER", "K_DPM_2"
samples = 1                      # number of images to generate
output_path = "sdxl_output.png"

endpoint = "https://api.stability.ai/v1/generation/stable-diffusion-xl-1024-v1-0/text-to-image"

headers = {
    "Authorization": f"Bearer {STABILITY_API_KEY}",
    "Content-Type": "application/json",
    "Accept": "image/png",
}

payload = {
    "text_prompts": [
        {"text": prompt, "weight": 1},
        {"text": negative_prompt, "weight": -1}
    ],
    "cfg_scale": cfg_scale,
    "steps": steps,
    "width": width,
    "height": height,
    "sampler": sampler,
    "samples": samples
}

resp = requests.post(endpoint, headers=headers, json=payload, timeout=120)

if resp.status_code == 200:
    with open(output_path, "wb") as f:
        f.write(resp.content)
    print(f"✅ Image saved to: {output_path}")
else:
    print("❌ Generation failed")
    print("Status:", resp.status_code)
    try:
        print("Response:", resp.json())
    except Exception:
        print("Response (raw):", resp.text[:500])


from PIL import Image
import matplotlib.pyplot as plt

img = Image.open(output_path)

plt.figure(figsize=(2, 2))  # ~passport-size in notebook
plt.imshow(img)
plt.axis("off")
plt.title("SDXL Preview")
plt.show()

Link copied!

Comments

Add Your Comment

Comment Added!