Loading...
Loading...
Generate high-quality AI images from text prompts or transform existing images using ModelsLab's API with 10,000+ models including FLUX, Realtime, and Community models. Supports text2img, img2img, inpainting, and ControlNet.
npx skill4agent add modelslab/skills modelslab-image-generationPOST https://modelslab.com/api/v6/realtime/text2imgPOST https://modelslab.com/api/v6/realtime/img2imgPOST https://modelslab.com/api/v6/images/text2imgPOST https://modelslab.com/api/v6/images/img2imgPOST https://modelslab.com/api/v6/images/inpaintPOST https://modelslab.com/api/v6/images/controlnetPOST https://modelslab.com/api/v6/images/fluximport requests
import time
def generate_image(prompt, api_key, api_type="community"):
"""Generate an image from text.
Args:
prompt: Text description of the image
api_key: Your ModelsLab API key
api_type: "realtime" (fast) or "community" (quality)
"""
if api_type == "realtime":
url = "https://modelslab.com/api/v6/realtime/text2img"
payload = {
"key": api_key,
"prompt": prompt,
"negative_prompt": "blurry, low quality",
"width": 512,
"height": 512,
"num_inference_steps": 20,
"guidance_scale": 7.5
}
else: # community
url = "https://modelslab.com/api/v6/images/text2img"
payload = {
"key": api_key,
"model_id": "midjourney", # or any model
"prompt": prompt,
"negative_prompt": "blurry, low quality, distorted",
"width": 768,
"height": 768,
"samples": 1,
"num_inference_steps": 30,
"guidance_scale": 7.5,
"safety_checker": "yes"
}
response = requests.post(url, json=payload)
data = response.json()
if data["status"] == "success":
return data["output"][0]
elif data["status"] == "processing":
# Poll for results (community API may be async)
return poll_result(data["id"], api_key)
else:
raise Exception(f"Error: {data.get('message')}")
def poll_result(request_id, api_key, timeout=300):
"""Poll for async generation results."""
start = time.time()
while time.time() - start < timeout:
resp = requests.post(
f"https://modelslab.com/api/v6/images/fetch/{request_id}",
json={"key": api_key}
)
data = resp.json()
if data["status"] == "success":
return data["output"][0]
elif data["status"] == "failed":
raise Exception(data.get("message", "Failed"))
time.sleep(5)
raise Exception("Timeout")
# Usage
image_url = generate_image(
"A futuristic cityscape at sunset, cyberpunk style, 8k, highly detailed",
"your_api_key",
api_type="community"
)
print(f"Image: {image_url}")def transform_image(init_image, prompt, api_key, strength=0.7):
"""Transform an existing image based on a prompt.
Args:
init_image: URL of the input image
prompt: How to transform the image
strength: 0.0-1.0, higher = more change
"""
response = requests.post(
"https://modelslab.com/api/v6/images/img2img",
json={
"key": api_key,
"model_id": "midjourney",
"prompt": prompt,
"init_image": init_image,
"strength": strength,
"width": 512,
"height": 512,
"num_inference_steps": 30,
"guidance_scale": 7.5
}
)
data = response.json()
if data["status"] == "success":
return data["output"][0]
elif data["status"] == "processing":
return poll_result(data["id"], api_key)
# Transform a photo into a painting
result = transform_image(
"https://example.com/photo.jpg",
"An oil painting in Van Gogh style",
"your_api_key",
strength=0.8
)def inpaint_image(image_url, mask_url, prompt, api_key):
"""Fill in masked parts of an image.
Args:
image_url: Original image URL
mask_url: Mask image URL (white = inpaint, black = keep)
prompt: What to generate in masked area
"""
response = requests.post(
"https://modelslab.com/api/v6/images/inpaint",
json={
"key": api_key,
"model_id": "midjourney",
"init_image": image_url,
"mask_image": mask_url,
"prompt": prompt,
"negative_prompt": "blurry, low quality",
"width": 512,
"height": 512,
"num_inference_steps": 30
}
)
data = response.json()
if data["status"] == "success":
return data["output"][0]
elif data["status"] == "processing":
return poll_result(data["id"], api_key)
# Replace object in image
result = inpaint_image(
"https://example.com/room.jpg",
"https://example.com/mask.jpg",
"A modern red sofa",
"your_api_key"
)def generate_with_controlnet(image_url, prompt, controlnet_type, api_key):
"""Use ControlNet for precise composition control.
ControlNet Types:
- canny: Edge detection
- depth: Depth map
- pose: Human pose
- scribble: Sketch-based
- normal: Surface normals
- seg: Semantic segmentation
"""
response = requests.post(
"https://modelslab.com/api/v6/images/controlnet",
json={
"key": api_key,
"model_id": "midjourney",
"controlnet_model": controlnet_type,
"controlnet_type": controlnet_type,
"init_image": image_url,
"prompt": prompt,
"width": 512,
"height": 512,
"num_inference_steps": 30
}
)
data = response.json()
if data["status"] == "success":
return data["output"][0]
elif data["status"] == "processing":
return poll_result(data["id"], api_key)
# Preserve pose, change style
result = generate_with_controlnet(
"https://example.com/person.jpg",
"A superhero in dynamic pose, comic book style",
"pose",
"your_api_key"
)realistic-vision-v13deliberate-v2absolute-reality-v1.6midjourneydreamshaper-8openjourneyanything-v5counterfeit-v2.5meinamix| Parameter | Description | Recommended Values |
|---|---|---|
| Text description of desired image | Be specific and detailed |
| What to avoid | "blurry, low quality, distorted" |
| Dimensions (divisible by 8) | 512, 768, 1024 |
| Number of images | 1-4 |
| Quality (higher = better) | 20 (fast), 30 (balanced), 50 (quality) |
| Prompt adherence | 7-8 (balanced), 10-15 (strict) |
| Reproducibility | |
| img2img change amount | 0.3 (subtle), 0.7 (moderate), 0.9 (heavy) |
| NSFW filter | "yes" or "no" |
✗ Bad: "a car"
✓ Good: "A red Ferrari sports car, studio lighting, highly detailed, 4k, photorealistic"
Include: Subject, style, lighting, quality descriptorsnegative_prompt = "blurry, low quality, distorted, deformed, ugly, bad anatomy, extra limbs"# Always check status and poll if needed
if data["status"] == "processing":
result = poll_result(data["id"], api_key)payload = {
"key": api_key,
"prompt": "...",
"webhook": "https://yourserver.com/webhook",
"track_id": "unique-identifier"
}image = generate_image(
"Professional product photo of wireless headphones, white background, studio lighting, commercial photography",
api_key,
negative_prompt="shadows, cluttered background, low quality"
)image = generate_image(
"Modern tech startup office, collaborative workspace, bright and airy, professional photography, 8k",
api_key
)image = generate_image(
"A serene Japanese garden at sunset, cherry blossoms, koi pond, peaceful atmosphere, oil painting style",
api_key,
api_type="community"
)seed = 12345
for i in range(4):
image = generate_image(
f"A cozy coffee shop interior, variation {i+1}",
api_key,
seed=seed
)try:
image = generate_image(prompt, api_key)
print(f"Success: {image}")
except Exception as e:
print(f"Generation failed: {e}")
# Log error, retry, or notify usermodelslab-image-editingmodelslab-video-generationmodelslab-webhooksmodelslab-sdk-usage