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 Reads

๐Ÿ“ฐ
Why PixelToolsPro is About to Become Your Next Go-To Image Editor
Discover PixelToolsPro, a fast and private image editor that doesn't require uploading images to a server, and learn how to use it for secure editing
Medium ยท AI
๐Ÿ“ฐ
I Couldn't Find a Good Image Metadata Tool, So I Built One
Learn how to build a custom image metadata tool to solve real-world problems and improve workflow efficiency
Dev.to ยท Robin Hood
๐Ÿ“ฐ
Building a Browser-Based Image Resizer with Step-Down Scaling and Crop
Learn to build a browser-based image resizer with step-down scaling and crop using interactive tools and social media presets
Dev.to ยท Arhan Ahmad
๐Ÿ“ฐ
Comment crรฉer des images professionnelles sans Photoshop avec l'IA
Create professional images without Photoshop using AI with Photopea and an AI agent
Dev.to ยท Mohamed Amine Ben Mallessa
Up next
I Let Google Omni AI Edit My Videos
Creator Magic
Watch โ†’