SuperAPI AI 工具集

imagen-generation

设计与创作

查看项目

更新于 10-16 10:47

技能介绍

用途

该技能面向需要将 Google Imagen 图像生成能力集成到 Python 工作流的场景。它基于 Vertex AI 平台,使用 ImageGenerationModel 完成以下任务:

  • 通过文本提示词生成图像
  • 用文本指令编辑已有图像(图生图)
  • 对图像的特定区域进行局部重绘(inpainting)或向外扩展(outpainting)
  • 使用 Imagen 放大模型提升图像分辨率
  • 将 AI 图像生成集成进 Python 工作流

环境准备

资料给出的前置依赖与配置如下:

pip install google-cloud-aiplatform pillow
gcloud auth application-default login
gcloud config set project YOUR_PROJECT_ID

启用 Vertex AI:

gcloud services enable aiplatform.googleapis.com

初始化与导入:

import vertexai
from vertexai.preview.vision_models import ImageGenerationModel

vertexai.init(project='YOUR_PROJECT_ID', location='us-central1')

主要能力

文本生成图像

使用 ImageGenerationModel.from_pretrained('imagegeneration@006') 加载模型,通过 generate_images 生成图像。可设置参数包括:

  • prompt:文本提示词
  • number_of_images:生成数量
  • aspect_ratio:宽高比,可选 '1:1'、'9:16'、'16:9'、'3:4'、'4:3'
  • guidance_scale:引导强度,范围 1-20,越高越贴近提示词
  • seed:可选,用于结果可复现

生成结果可通过 response.images[0].save('output.png') 保存,也可转换为 PIL 图像查看。

资料还给出了批量生成示例:对一组提示词逐一调用生成并保存为不同文件。

图像编辑(图生图)

使用 edit_image 方法,传入 base_image 与 prompt,并设置:

  • edit_mode:'inpainting-insert'、'inpainting-remove' 或 'outpainting'
  • mask_mode:'background'、'foreground' 或 'semantic'
  • number_of_images、guidance_scale

示例用法为将天空编辑为带风暴云的效果。

带遮罩的局部重绘

资料展示了用 PIL 创建遮罩(白色为待重绘区域,黑色为保留区域)并保存为 mask.png,然后将其作为 mask 传入 edit_image,配合 edit_mode='inpainting-insert' 与提示词完成区域替换。

图像放大

使用 upscale_image 方法,传入 image 与 upscale_factor('x2' 或 'x4'),返回结果可保存。

负面提示词

generate_images 支持 negative_prompt 参数,用于排除不需要的元素。示例中使用的负面提示词包括 blurry、low quality、cartoon、anime、watermark、text、logo 等。

Imagen 3

资料介绍最新模型 imagen-3.0-generate-001 具备最高质量与最佳提示词遵循度,支持 safety_filter_level('block_most'、'block_some'、'block_few')与 person_generation('dont_allow'、'allow_adult')等参数。

模型参考

资料列出以下模型及其用途与说明:

模型 ID 用途 说明
imagen-3.0-generate-001 最高质量生成 最新,最佳提示词遵循
imagen-3.0-fast-generate-001 快速/高性价比生成 更低延迟
imagegeneration@006 稳定的生产模型 经过充分测试
imagegeneration@005 上一代 旧版
imagen-3.0-capability-001 编辑与转换 局部重绘、向外扩展

不依赖 GCP 的替代方案

资料提供基于 Hugging Face diffusers 的 Stable Diffusion 本地方案,无需 GCP:

pip install diffusers transformers accelerate torch
from diffusers import StableDiffusionPipeline
import torch

pipe = StableDiffusionPipeline.from_pretrained(
    'runwayml/stable-diffusion-v1-5',
    torch_dtype=torch.float16,
)
pipe = pipe.to('cuda')  # or 'cpu' (slow)

image = pipe(
    prompt='A futuristic city at sunset',
    negative_prompt='blurry, low quality',
    num_inference_steps=30,
    guidance_scale=7.5,
).images[0]

image.save('output.png')

适用场景

  • 需要在 Python 中批量生成图像或进行图像编辑的开发者
  • 需要局部重绘或图像放大能力的图像处理流程
  • 希望使用 Google Vertex AI 上 Imagen 模型的项目
  • 没有 GCP 环境、希望本地运行 Stable Diffusion 的场景

限制与合规

  • Imagen 遵循 Google 内容政策,显式或有害内容请求会被阻止
  • safety_filter_level 控制严格程度:block_most(最安全)到 block_few(较宽松)
  • person_generation='dont_allow' 可禁用人脸生成,以满足儿童安全合规要求
  • 资料建议存储响应中的 generation_parameters 以满足审计/可复现要求
  • 不应在未进行适当同意处理的情况下生成真实人物的图像

成本优化

资料给出的建议包括:迭代/草稿使用 imagen-3.0-fast-generate-001,最终输出切换到 imagen-3.0-generate-001;开发阶段每次调用生成 1-2 张,仅在生成环境批量处理;对重复提示词缓存结果;使用 seed 保证可复现以避免重复生成。定价需查看 cloud.google.com/vertex-ai/pricing(按图像计费)。

以上信息均来自资料原文,未提及的价格数值、平台支持范围等细节暂未提供。