跳转至

文本生图像

介绍

图像生成结合了深度学习和生成对抗网络(GANs)来创建令人惊叹和逼真的图像。它在一个大型的多样化图像数据集上进行训练,并可以根据给定的提示或概念生成新的图像。

使用图像生成模型,你可以释放你的创造力,探索图像生成的可能性。无论你是想创建独特的艺术作品,设计新产品,或者只是想玩玩视觉实验,图像生成都提供了一个多功能和直观的平台。

在这个指南中,我们以OpenAI为例,将探测DALL-E的基本原理,并学习如何使用这种尖端技术生成图像。我们将介绍基本概念,生成图像的过程,并提供一些例子,帮助你开始自己的图像生成之旅。

让我们深入了解OpenAI DALL-E图像生成的迷人世界吧!

最佳实践

当你想要使用API来生成图像时,你可以使用以下代码为你的文本数据生成图像:

curl -X POST "https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN" -H "Content-Type: application/json" 
-H "Authorization: Bearer YOUR_ACCESS_KEY"
-d '{
    "prompt": "A cute baby sea otter",
    "n": 1,
    "size": "1024x1024" 
}

您可以在请求中指定的参数包括:

  • prompt:您想要生成图像的输入文本或概念。
  • n(可选):要生成的图像数量,默认值为1。对于dall-e-3,只支持n=1。
  • quality(可选):生成的图像的质量(例如,"standard"或"hd")。这是一个可选参数,默认值为"standard"。 "hd"只支持dall-e-3。
  • response_format(可选):响应的格式(例如,"url"或"b64_json")。这是一个可选参数,默认值为"url"。
  • size(可选):生成的图像的大小(例如,对于dall-e-2,"256x256","512x512","1024x1024",对于dall-e-3,"1024x1024","1792x1024","1024x1792")。
  • style(可选):生成的图像的风格(例如,"vivid"或"natural")。这是一个可选参数,默认值为"vivid"。这个参数只适用于dall-e-3
  • user(可选):发出请求的用户的用户id。

发送请求后,您将收到一个包含基于输入文本或概念生成的图像的响应。然后,您可以使用这些图像进行各种创意项目,设计任务,或视觉探索。

无缝集成

您也可以使用Python OpenAI库将图像生成过程集成到您的应用程序中。以下是一个示例代码片段:

from openai import OpenAI

client = OpenAI(
    base_url="https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN",
    api_key='YOUR_API_KEY'
)

response = client.images.generate(
    prompt="generate a cat.",
    n=1
)

print(response)

URL的回复会如下:

{
    "created": 1632345678,
    "data": [
        {
            "url": "https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN/images/1" // maybe the native url of OpenAI or Azure OpenAI
        }
    ]
}

URL将是OpenAI或Azure OpenAI的原生URL,因此您应确认您的网络环境可以访问该URL。

如果您想使用bson格式,您可以在请求中将response_format参数设置为"b64_json"。

from openai import OpenAI
import base64
from PIL import Image
from io import BytesIO

client = OpenAI(
    base_url="https://genaiapi.cloudsway.net/v1/ai/zUcfeMfrpNqyEhTN",
    api_key='YOUR_API_KEY'
)

response = client.images.generate(
    prompt="generate a cat.",
    n=1,
    response_format="b64_json"
)

# print(response)
# Get b64_json data from response
b64_json_data = response.data[0].b64_json

# Convert b64_json data to png image
image_data = base64.b64decode(b64_json_data)
image = Image.open(BytesIO(image_data))

# Save the image as png
image.save('./image.png')

图像将以image.png的形式保存在当前目录中。但是,您应该知道,bs64_json格式的数据将比url格式消耗更多的数据传输。

MaaS_Ge_2.5_flash_image_preview 生图调用示例

Gemini原生协议调用

1. 文生图

curl --location 'https://genaiapi.cloudsway.net/v1/ai/{ENDPOINT_PATH}/generateContent' \
--header 'Authorization: Bearer {YOUR_ACCESS_KEY}' \
--header 'Content-Type: application/json' \
--data '{
    "contents": [
        {
            "role": "USER",
            "parts": {
                "text": "生成一张电影效果照片,照片中有一栋大型建筑,建筑正面投影着巨大的文字:‘Gemini 2.5 现在可以生成长篇文本了"
            }
        }
    ],
    "generationConfig": {

        "response_modalities": [
            "TEXT",
            "IMAGE"
        ]
    }
}'

2. 文生图-流式

curl --location 'https://genaiapi.cloudsway.net/v1/ai/{ENDPOINT_PATH}/streamGenerateContent' \
--header 'Authorization: Bearer {YOUR_ACCESS_KEY}' \
--header 'Content-Type: application/json' \
--data '{
    "contents": [
        {
            "role": "USER",
            "parts": {
                "text": "生成一张电影效果照片,照片中有一栋大型建筑,建筑正面投影着巨大的文字:‘Gemini 2.5 现在可以生成长篇文本了"
            }
        }
    ],
    "generationConfig": {

        "response_modalities": [
            "TEXT",
            "IMAGE"
        ]
    }
}'

3. 图生图

curl --location 'https://genaiapi.cloudsway.net/v1/ai/{ENDPOINT_PATH}/generateContent' \
--header 'Authorization:  Bearer {YOUR_ACCESS_KEY}' \
--header 'Content-Type:  application/json' \
--data '{
    "contents": [
        {
            "role": "USER",
            "parts": [
                {
                    "text": "{你的提示词文本}"
                },
                {
                    "inlineData": {
                        "data": "{你的图片base64编码数据}",
                        "mimeType": "image/png"
                    }
                },
                 {
                    "inlineData": {
                        "data": "{你的图片base64编码数据}",
                        "mimeType": "image/png"
                    }
                }
            ]
        }
    ],
    "safetySettings": [
        {
            "category": "HARM_CATEGORY_HARASSMENT",
            "threshold": "OFF"
        },
        {
            "category": "HARM_CATEGORY_HATE_SPEECH",
            "threshold": "OFF"
        },
        {
            "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
            "threshold": "OFF"
        },
        {
            "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
            "threshold": "OFF"
        }
    ],
    "generationConfig": {
        "response_modalities": [
            "TEXT",
            "IMAGE"
        ]
    }
}'

openai协议调用

4. openai格式:文生图

curl --request POST \
  --url https://genaiapi.cloudsway.net/v1/ai/{ENDPOINT_PATH}/chat/completions \
  --header 'Accept: */*' \
  --header 'Accept-Encoding: gzip, deflate, br' \
  --header 'Authorization: Bearer {YOUR_ACCESS_KEY}' \
  --header 'Connection: keep-alive' \
  --header 'Content-Type: application/json' \
  --header 'User-Agent: PostmanRuntime-ApipostRuntime/1.1.0' \
  --data '{
    "model": "google/gemini-2.5-flash-image-preview",
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Create a tutorial explaining how to make scrambled eggs with tomatoes in three steps."
                }
        ]
    }
]
}'

5. openai格式:文生图-流式

curl --request POST \
  --url https://genaiapi.cloudsway.net/v1/ai/{ENDPOINT_PATH}/chat/completions \
  --header 'Accept: */*' \
  --header 'Accept-Encoding: gzip, deflate, br' \
  --header 'Authorization: Bearer {YOUR_ACCESS_KEY}' \
  --header 'Connection: keep-alive' \
  --header 'Content-Type: application/json' \
  --header 'User-Agent: PostmanRuntime-ApipostRuntime/1.1.0' \
  --data '{
    "model": "google/gemini-2.5-flash-image-preview",
    "stream": true,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Create a tutorial explaining how to make scrambled eggs with tomatoes in three steps."
                }
        ]
    }
]
}'

6. openai格式:图生图

curl --location 'https://genaiapi.cloudsway.net/v1/ai/{ENDPOINT}/chat/completions' \
--header 'Authorization:  Bearer {YOUR_ACCESS_KEY}' \
--header 'Content-Type:  application/json' \
--data '{
  "model": "google/gemini-2.5-flash-image-preview",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "Create a tutorial explaining how to make scrambled eggs with tomatoes in three steps."
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "data:image/png;base64,{data}"
           }
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "data:image/png;base64,{data}"
           }
        }
      ]
    }
  ]

}'