Files
dsProject/dsLightRag/Test/QWenImage/T1_QwenImageSync.py
2025-08-27 08:04:00 +08:00

31 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
# pip install -U dashscope
from dashscope import ImageSynthesis
import os
prompt = "一副典雅庄重的对联悬挂于厅堂之中,房间是个安静古典的中式布置,桌子上放着一些青花瓷,对联上左书“义本生知人机同道善思新”,右书“通云赋智乾坤启数高志远”, 横批“智启通义”,字体飘逸,中间挂在一着一副中国风的画作,内容是岳阳楼。"
# 若没有配置环境变量请用百炼API Key将下行替换为api_key="sk-xxx"
api_key = os.getenv("DASHSCOPE_API_KEY")
print('----同步调用,请等待任务执行----')
rsp = ImageSynthesis.call(api_key=api_key,
model="qwen-image",
prompt=prompt,
n=1,
size='1328*1328')
print('response: %s' % rsp)
if rsp.status_code == HTTPStatus.OK:
# 在当前目录下保存图片
for result in rsp.output.results:
file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
with open('./%s' % file_name, 'wb+') as f:
f.write(requests.get(result.url).content)
else:
print('同步调用失败, status_code: %s, code: %s, message: %s' %
(rsp.status_code, rsp.code, rsp.message))