下面的代码是一个使用 Python 编写的脚本,用于调用阿里云的 Stable Diffusion 模型(通过 Dashscope 库)来生成图像。这个脚本显示了如何使用 Dashscope 的 API,根据用户提供的提示语句生成图像,并将它们保存到指定的路径。
from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import Path
from pathlib import PurePosixPath
import requests
import dashscope# 阿里SD,500张,用完需再申请=5176.28197632.0.0.97d87e06OPIVDX&disableWebsiteRedirect=truedashscope.api_key = ''# model = "dashscope.ImageSynthesis.Models.wanx_v1"def generate_and_save_images(prompt, n, size, save_path):rsp = dashscope.ImageSynthesis.call(model=dashscope.ImageSynthesis.Models.wanx_v1,prompt=prompt,n=n,size=size)if rsp.status_code == HTTPStatus.OK:print(rsp.output)print(rsp.usage)for result in sults:file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]save_file_path = Path(save_path) / file_namewith open(save_file_path, 'wb+') as f:f.(result.url).content)else:print('Failed, status_code: %s, code: %s, message: %s' %(rsp.status_code, de, ssage))if __name__ == '__main__':prompt = "a dog and a cat"n = 4size = '1024*1024'save_path = 'D:\wenjian\临时'generate_and_save_images(prompt, n, size, save_path)
这个脚本可以广泛应用于需要自动生成图像的场景,如:
本文发布于:2024-02-01 21:53:22,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170679560439653.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |