mcpField
M
modelslab-image-generation

modelslab-image-generation

github.com/modelslab创意与媒体1372 次安装36 周前更新
代码仓库

关于 modelslab-image-generation

modelslab-image-generation 是使用 ModelsLab API 生成和转换图像的技能,面向需要文本生图或图像编辑的创作者。它支持多种模型,并涵盖局部重绘与异步结果获取。

主要功能

  1. 01文本生成图像
  2. 02图像到图像转换
  3. 03蒙版区域局部重绘
  4. 04搜索和选择图像模型
  5. 05轮询异步生成结果

适用场景

  1. 01根据文本描述制作营销图
  2. 02基于现有图像生成变体
  3. 03填充图像中的蒙版区域

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/modelslab/skills --skill modelslab-image-generation

安装后在 Claude Code 中输入/modelslab-image-generation确认已加载。

详细介绍

ModelsLab 图像生成

使用 ModelsLab 丰富的模型库和强大的生成端点,生成令人惊艳的 AI 图像。

何时使用此技能

  • 根据文本描述生成图像
  • 转换或修改现有图像
  • 填充图像中的蒙版区域(修复)
  • 使用 ControlNet 实现精准控制
  • 需要快速生成(Realtime API)
  • 需要最高画质(Community/FLUX 模型)
  • 创建产品图像、营销图形或艺术作品

可用 API(v7)

1. 文生图
  • POST https://modelslab.com/api/v7/images/text-to-image
  • 可用模型超过 50,000 个
2. 图生图
  • POST https://modelslab.com/api/v7/images/image-to-image
  • 根据提示词转换现有图像
3. 图像修复
  • POST https://modelslab.com/api/v7/images/inpaint
  • 填充图像的蒙版区域
4. 获取结果
  • POST https://modelslab.com/api/v7/images/fetch/{id}
  • 轮询异步生成结果

注意:v6 端点(/api/v6/images/text2img 等)仍可使用,但 v7 是当前版本。

查找图像模型

# Search image models by name
modelslab models search --feature imagen

# Search specific models
modelslab models search --search "flux"
modelslab models search --search "midjourney"

# Get model details
modelslab models detail --id flux

快速入门:文生图

import requests
import time

def generate_image(prompt, api_key, model_id="flux"):
    """Generate an image from text.

    Args:
        prompt: Text description of the image
        api_key: Your ModelsLab API key
        model_id: Model to use (flux, midjourney, sdxl, imagen-3, etc.)
    """
    response = requests.post(
        "https://modelslab.com/api/v7/images/text-to-image",
        json={
            "key": api_key,
            "model_id": model_id,
            "prompt": prompt,
            "negative_prompt": "blurry, low quality, distorted",
            "width": 1024,
            "height": 1024,
            "samples": 1,
            "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)
    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/v7/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",
    model_id="flux"
)
print(f"Image: {image_url}")

图生图转换

def transform_image(init_image, prompt, api_key, model_id="flux", 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
        model_id: Model to use (flux models don't need scheduler, SD models do)
        strength: 0.0-1.0, higher = more change
    """
    payload = {
        "key": api_key,
        "model_id": model_id,
        "prompt": prompt,
        "init_image": [init_image],  # v7 expects array
        "strength": strength,
        "width": 512,
        "height": 512
    }

    response = requests.post(
        "https://modelslab.com/api/v7/images/image-to-image",
        json=payload
    )

    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",
    model_id="flux",
    strength=0.8
)

图像修复(填充蒙版区域)

def inpaint_image(image_url, mask_url, prompt, api_key, model_id="flux"):
    """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
        model_id: Model to use
    """
    response = requests.post(
        "https://modelslab.com/api/v7/images/inpaint",
        json={
            "key": api_key,
            "model_id": model_id,
            "init_image": image_url,
            "mask_image": mask_url,
            "prompt": prompt,
            "negative_prompt": "blurry, low quality"
        }
    )

    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",
    model_id="flux"
)

常用模型 ID

高级 / 快速
  • flux - Flux Dev(速度快、画质高,无需调度器)
  • imagen-3 - Google Imagen 3(照片级真实感)
  • imagen-4 - Google Imagen 4(最新版本)
照片级真实感
  • realistic-vision-v13 - 高质量写实图像
  • deliberate-v2 - 多用途写实风格
  • absolute-reality-v1.6 - 超写实输出
艺术 / 风格化
  • midjourney - MidJourney 风格图像(最受欢迎)
  • dreamshaper-8 - 富有艺术感且用途广泛
  • openjourney - 开源的 MidJourney 替代方案
动漫 / 插画
  • anything-v5 - 动漫风格
  • counterfeit-v2.5 - 动漫肖像
  • meinamix - 高质量动漫艺术作品

浏览所有模型:https://modelslab.com/models

关键参数

参数描述推荐值
model_id要使用的模型(必填)flux、midjourney、sdxl 等
prompt所需图像的文本描述尽量具体、详细
negative_prompt需要避免的内容"模糊、低质量、失真"
width / height尺寸(512-1024)512、768、1024
samples图像数量1-4
guidance_scale提示词遵循程度7-8(均衡),10-15(严格)
seed可复现性null(随机)或数字
strength图生图的变化幅度0.3(轻微)、0.7(中等)、0.9(较大)
webhook异步通知 URLURL 字符串
track_id自定义跟踪标识符任意字符串

最佳实践

1. 编写有效的提示词
Bad: "a car"
Good: "A red Ferrari sports car, studio lighting, highly detailed, 4k, photorealistic"

Include: Subject, style, lighting, quality descriptors
2. 使用负面提示词
negative_prompt = "blurry, low quality, distorted, deformed, ugly, bad anatomy, extra limbs"
3. 选择合适的模型
  • flux:速度快、画质高,是不错的默认选择
  • midjourney:艺术感强,输出风格化
  • imagen-3/4:高级照片级真实感(Google)
  • sdxl:用途广泛的基础模型,提供多种微调版本
4. 处理异步操作
# Always check status and poll if needed
if data["status"] == "processing":
    result = poll_result(data["id"], api_key)
5. 对耗时较长的操作使用 Webhook
payload = {
    "key": api_key,
    "prompt": "...",
    "webhook": "https://yourserver.com/webhook",
    "track_id": "unique-identifier"
}

错误处理

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 user

资源

相关技能

  • modelslab-model-discovery - 查找和筛选模型
  • modelslab-image-editing - 编辑和增强图像
  • modelslab-video-generation - 根据图像生成视频
  • modelslab-chat-generation - 与 LLM 模型聊天
  • modelslab-webhooks - 处理异步操作
  • modelslab-sdk-usage - 使用官方 SDK

评论

支持 Markdown