腾讯云AI绘画测试代码

2023-07-17 14:12:31 浏览数 (1)

腾讯云AI绘画文生图个人测试。Python语言

腾讯云也推出了AI绘画 支持一句话生成图片 图片生成图片 下面就给大家给一段已经测试并且拿来就能用的python 代码 想测试的朋友都可以来尝试一下腾讯云的AI绘画

代码很简单 也很简陋没有美化 就是单纯用来测试的


Flask框架的程序 需要先创建个Flask的项目


这个代码是 app 的代码

代码语言:javascript复制
from flask import Flask, render_template, request, redirect, url_for
import json
import base64
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.aiart.v20221229 import aiart_client, models

app = Flask(__name__)

SECRET_ID = "" # AI绘画ID
SECRET_KEY = "" # AI绘画密匙


REGION = "ap-guangzhou"  #AI绘画地区
ENDPOINT = "aiart.tencentcloudapi.com"

cred = credential.Credential(SECRET_ID, SECRET_KEY)
httpProfile = HttpProfile()
httpProfile.endpoint = ENDPOINT
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
client = aiart_client.AiartClient(cred, REGION, clientProfile)


@app.route('/', methods=['GET', 'POST'])
def text_to_image():
    if request.method == 'POST':
        prompt = request.form['prompt']
        negative_prompt = request.form['negative_prompt']
        styles = request.form.getlist('styles')
        try:
            req = models.TextToImageRequest()
            params = {
                "Prompt": prompt,
                "NegativePrompt": negative_prompt,
                "Styles": styles
            }
            req.from_json_string(json.dumps(params))
            resp = client.TextToImage(req)
            image_data = resp.ResultImage
            image_path = "static/image.png"
            with open(image_path, "wb") as file:
                file.write(base64.b64decode(image_data))

            return redirect(url_for('result'))

        except TencentCloudSDKException as err:
            return render_template('index.html', error_message=str(err))

    return render_template('index.html')


@app.route('/result')
def result():
    image_path = "static/images/image.png"
    return render_template('result.html', image_path=image_path)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9797)

下面是HTML界面代码 用于输入文字描述 选择风格 然后显示照片的


index.html

代码语言:javascript复制
<!DOCTYPE html>
<html>
<head>
    <title>腾讯云AI文生图</title>
</head>
<body style="text-align: center">
    <h1>腾讯云AI文生图</h1>
    {% if error_message %}
        <p>{{ error_message }}</p>
    {% endif %}
    <form method="POST">
        <label for="prompt">图片描述:</label>
        <input type="text" id="prompt" name="prompt" placeholder="图片中文描述">
        <br>
        <label for="negative_prompt">反向图片描述:</label>
        <input type="text" id="negative_prompt" name="negative_prompt" placeholder="图片中不想出现的元素">
        <br>
        <label for="styles">图片风格:</label>
        <select id="styles" name="styles" multiple>
            <option value="101">水墨画</option>
            <option value="102">概念艺术</option>
            <option value="103">油画</option>
            <option value="104">水彩画</option>
            <option value="106">厚涂风格</option>
            <option value="107">插画</option>
            <option value="108">剪纸风格</option>
            <option value="109">印象派</option>
            <option value="110">2.5D人像</option>
            <option value="111">肖像画</option>
            <option value="112">黑白素描画</option>
            <option value="113">赛格朋克</option>
            <option value="114">科幻风格</option>
            <option value="201">日系动漫</option>
            <option value="202">怪兽风格</option>
            <option value="301">游戏卡通手绘</option>
            <option value="000">不限定风格</option>
        </select>
        <br>
        <input type="submit" value="生成图片">
    </form>
</body>
</html>

show.html 展示页面

代码语言:javascript复制
<!DOCTYPE html>
<html>
<head>
    <title>AI图片</title>
</head>
<body>
<h1>AI以为您生成图片</h1>
    <h2><a href="/">
  <button>文生图</button>
</a></h2>
    <img src="{{ url_for('static', filename='image.png') }}" alt="Generated Image">

</body>
</html>

0 人点赞