AI image generation has revolutionized digital art. Learn to create amazing images with leading AI tools.
DALL-E 3 via OpenAI API
import openai
# Generate image
response = openai.images.generate(
model="dall-e-3",
prompt="A serene Japanese garden with cherry blossoms, koi pond, and stone lanterns, digital art style, high quality",
size="1024x1024",
quality="standard", # or "hd"
n=1
)
image_url = response.data[0].url
print(f"Image URL: {image_url}")
# Save image
import requests
from PIL import Image
from io import BytesIO
img_response = requests.get(image_url)
img = Image.open(BytesIO(img_response.content))
img.save("generated_image.png")
```
Image Variations
# Create variations of existing image
with open("original.png", "rb") as image_file:
response = openai.images.create_variation(
image=image_file,
n=3,
size="1024x1024"
)
for i, img in enumerate(response.data):
print(f"Variation {i+1}: {img.url}")
```
Effective Image Prompting
# Structure: Subject + Style + Details + Quality
Good Prompt:
"A majestic lion on a mountain peak at sunset,
realistic photography style, dramatic lighting,
golden hour, high detail, 4k quality"
Include:
- Subject (what): lion, mountain, sunset
- Style: realistic photography, digital art, oil painting
- Details: dramatic lighting, colors
- Technical: 4k, high detail, professional
Avoid:
- Vague: "a nice picture of an animal"
- Too many concepts: confuses the model
```
Midjourney-Style Prompts
/imagine prompt: cyberpunk cityscape,
neon lights, flying cars, rainy night,
blade runner style, highly detailed,
cinematic lighting, 8k --ar 16:9 --q 2
Parameters:
--ar 16:9 (aspect ratio)
--q 2 (quality)
--s 750 (stylize)
--v 5.2 (version)
```
Stable Diffusion with Diffusers
from diffusers import StableDiffusionPipeline
import torch
# Load model
model_id = "stabilityai/stable-diffusion-2-1"
pipe = StableDiffusionPipeline.from_pretrained(
model_id,
torch_dtype=torch.float16
)
pipe = pipe.to("cuda") # Use GPU
# Generate image
prompt = "A futuristic city with flying cars, cyberpunk style"
negative_prompt = "blurry, low quality, distorted"
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=50,
guidance_scale=7.5
).images[0]
image.save("output.png")
```
Advanced Techniques
# Image-to-Image
from diffusers import StableDiffusionImg2ImgPipeline
pipe_img2img = StableDiffusionImg2ImgPipeline.from_pretrained(model_id)
init_image = Image.open("sketch.png")
transformed = pipe_img2img(
prompt="A photo-realistic version of this sketch",
image=init_image,
strength=0.75 # How much to transform
).images[0]
# Inpainting (edit parts of image)
from diffusers import StableDiffusionInpaintPipeline
pipe_inpaint = StableDiffusionInpaintPipeline.from_pretrained(model_id)
result = pipe_inpaint(
prompt="Replace with a red sports car",
image=original_image,
mask_image=mask # White where to edit
).images[0]
```
AI image generation opens endless creative possibilities. Experiment and create!