Build AI Image Generator in Python | Text to Image with Stable Diffusion

CodeVisium ยท Intermediate ยท๐ŸŽจ Image & Video AI ยท3mo ago

About this lesson

Welcome to AI & Data Skills Shorts by CodeVisium ๐Ÿš€ In this project you will build a Text-to-Image AI Generator using Python. The application takes a text prompt and generates an AI image using a model like Stable Diffusion. You will learn: How AI image generation works How to call AI APIs using Python How to save generated images How to create a simple web UI ๐Ÿ”น Step 1: Install Required Libraries Run: pip install requests pillow streamlit Libraries used: requests Used to send API requests. pillow (PIL) Used for handling images. streamlit Used for building a quick web interface. ๐Ÿ”น Step 2: Create Text Prompt A prompt describes what image you want. Example: prompt = "astronaut riding a horse in space" Better prompts give better images. Examples: futuristic city skyline cyberpunk robot warrior fantasy castle on floating island ๐Ÿ”น Step 3: Generate Image using API Example Python script: import requests api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2" headers = { "Authorization": "Bearer YOUR_API_KEY" } data = { "inputs": "A futuristic neon city at night" } response = requests.post(api_url, headers=headers, json=data) with open("generated.png", "wb") as f: f.write(response.content) This code: Sends text prompt to AI model Model generates image Image is saved locally ๐Ÿ”น Step 4: Open Generated Image Using Python: from PIL import Image img = Image.open("generated.png") img.show() This will display the generated AI artwork. ๐Ÿ”น Step 5: Build Web Interface with Streamlit Create file app.py import streamlit as st import requests st.title("AI Image Generator") prompt = st.text_input("Enter prompt") if st.button("Generate Image"): response = requests.post( api_url, headers=headers, json={"inputs": prompt} ) st.image(response.content) Run app: streamlit run app.py Now you have a simple AI image generator web app. ๐Ÿ“‚ Tools Used in This Project Py

Original Description

Welcome to AI & Data Skills Shorts by CodeVisium ๐Ÿš€ In this project you will build a Text-to-Image AI Generator using Python. The application takes a text prompt and generates an AI image using a model like Stable Diffusion. You will learn: How AI image generation works How to call AI APIs using Python How to save generated images How to create a simple web UI ๐Ÿ”น Step 1: Install Required Libraries Run: pip install requests pillow streamlit Libraries used: requests Used to send API requests. pillow (PIL) Used for handling images. streamlit Used for building a quick web interface. ๐Ÿ”น Step 2: Create Text Prompt A prompt describes what image you want. Example: prompt = "astronaut riding a horse in space" Better prompts give better images. Examples: futuristic city skyline cyberpunk robot warrior fantasy castle on floating island ๐Ÿ”น Step 3: Generate Image using API Example Python script: import requests api_url = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2" headers = { "Authorization": "Bearer YOUR_API_KEY" } data = { "inputs": "A futuristic neon city at night" } response = requests.post(api_url, headers=headers, json=data) with open("generated.png", "wb") as f: f.write(response.content) This code: Sends text prompt to AI model Model generates image Image is saved locally ๐Ÿ”น Step 4: Open Generated Image Using Python: from PIL import Image img = Image.open("generated.png") img.show() This will display the generated AI artwork. ๐Ÿ”น Step 5: Build Web Interface with Streamlit Create file app.py import streamlit as st import requests st.title("AI Image Generator") prompt = st.text_input("Enter prompt") if st.button("Generate Image"): response = requests.post( api_url, headers=headers, json={"inputs": prompt} ) st.image(response.content) Run app: streamlit run app.py Now you have a simple AI image generator web app. ๐Ÿ“‚ Tools Used in This Project Py
Watch on YouTube โ†— (saves to browser)
Sign in to unlock AI tutor explanation ยท โšก30

Related AI Lessons

โšก
FREE AI Sin City Photo Generator โ€” Turn Any Photo Into High-Contrast Noir Art (2026)
Transform any photo into a Sin City-inspired high-contrast noir art using a free AI generator
Dev.to AI
โšก
Google makes Geminiโ€™s personalized image generation free for all US users
Google's Gemini personalized image generation is now free for all US users, allowing them to generate images informed by their Google data
The Next Web AI
โšก
Geminiโ€™s personalized AI image generation is now free for U.S. users
Gemini's AI image generation is now free for U.S. users, allowing for personalized images based on user interests and data
TechCrunch AI
โšก
WebP's Compression Secret: How a 1MB PNG Becomes a 200KB WebP
Learn how WebP compresses images more efficiently than PNG and JPEG, and why it matters for web development
Dev.to ยท swift king
Up next
OpenAI Kills Sora then Descends into Chaos
ColdFusion
Watch โ†’