Gen AI — Image Generation
DALL-E
response = client.images.generate(
model="dall-e-3",
prompt="A futuristic city at sunset, digital art",
size="1024x1024",
quality="standard",
n=1
)
image_url = response.data[0].url
Image editing
response = client.images.edit(
model="dall-e-2",
image=open("original.png", "rb"),
mask=open("mask.png", "rb"),
prompt="Add a cat sitting on the desk"
)
Variations
response = client.images.create_variation(
model="dall-e-2",
image=open("original.png", "rb"),
n=3,
size="1024x1024"
)
Prompt tips
# Be specific
"A professional headshot of a woman in a blue blazer,
neutral background, studio lighting, 4K"
# Use style references
"Oil painting of a mountain landscape,
in the style of Monet, vibrant colors"
# Include composition
"Close-up macro photo of a dewdrop on a leaf,
shallow depth of field, morning light"
Stable Diffusion
from diffusers import StableDiffusionPipeline
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5"
)
pipe = pipe.to("cuda")
image = pipe("A cat wearing sunglasses").images[0]
image.save("cat.png")
Mini Practice
- Generate an image with DALL-E
- Create variations
- Edit an existing image
- Experiment with prompts
Up Next
Continue with Audio Generation - Text to speech.
Related Topics
Frequently Asked Questions about Image Generation
What is Image Generation in Gen AI?
Image Generation is a fundamental concept in Gen AI. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Image Generation?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Image Generation.
Why is Image Generation important in Gen AI?
Image Generation is essential for Gen AI development. Understanding this concept will help you write better code and solve real-world problems more effectively.