阿里通义千问调用图像大模型生成轮动漫风格 python调用

首先要安装环境 需要安装这个 dashscope

复制代码
pip install -U dashscope

出现

表示安装成功了

接着可以写脚本了

复制代码
#pip install -U dashscope 需要安装这个

import os
import base64
import mimetypes
from dashscope import MultiModalConversation
import dashscope

# 设置API Key(请替换为你的实际API Key)
api_key = "sk-xxxxxxxxx"

def encode_file(file_path):
    """将图片文件编码为Base64"""
    mime_type, _ = mimetypes.guess_type(file_path)
    if not mime_type or not mime_type.startswith("image/"):
        raise ValueError("不支持的图像格式")

    with open(file_path, "rb") as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
    return f"data:{mime_type};base64,{encoded_string}"

def generate_lineart_with_wanx(input_path, output_path):
    """使用通义万相生成线稿"""
    try:
        # 编码图片
        image_base64 = encode_file(input_path)
        
        # 构建消息
        messages = [
            {
                "role": "user",
                "content": [
                    {"image": image_base64},
                    {"text": "将这张图片转换动漫风格的图片"}
                ]
            }
        ]
        
        # 调用API
        response = MultiModalConversation.call(
            api_key=api_key,
            model="qwen-image-edit-plus",
            messages=messages,
            stream=False,
            n=1,
            size="1280*1280",
            watermark=False
        )
        
        if response.status_code == 200:
            # 获取生成的图片URL
            image_url = response.output.choices[0].message.content[0]['image']
            print(f"生成成功,图片URL: {image_url}")
            # 注意:这里需要另外编写下载图片的代码
            return True
        else:
            print(f"API调用失败: {response.code} - {response.message}")
            return False
            
    except Exception as e:
        print(f"处理错误: {str(e)}")
        return False

# 使用示例
generate_lineart_with_wanx("666999.png", "666999_al_line.png")

接着执行

输入图片

我输的关键词是 "将这张图片转换动漫风格的图片"

生成图片

看官方写着好像是2毛一张,还是三毛一张 ,免费额度100张,有点小贵

相关推荐
moshuying1 天前
别让AI焦虑,偷走你本该有的底气
前端·人工智能
GIS之路1 天前
ArcPy,一个基于 Python 的 GIS 开发库简介
前端
可夫小子1 天前
OpenClaw基础-为什么会有两个端口
前端
喝拿铁写前端1 天前
Dify 构建 FE 工作流:前端团队可复用 AI 工作流实战
前端·人工智能
用户8356290780511 天前
无需 Office:Python 批量转换 PPT 为图片
后端·python
喝咖啡的女孩1 天前
React 合成事件系统
前端
从文处安1 天前
「九九八十一难」组合式函数到底有什么用?
前端·vue.js
用户5962585736061 天前
戴上AI眼镜逛花市——感受不一样的体验
前端
yuki_uix1 天前
Props、Context、EventBus、状态管理:组件通信方案选择指南
前端·javascript·react.js
老板我改不动了1 天前
前端面试复习指南【代码演示多多版】之——HTML
前端