阿里通义千问调用图像大模型生成轮动漫风格 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张,有点小贵

相关推荐
vortex520 小时前
Bash Shell 的展开与补全机制
开发语言·bash
行走的陀螺仪20 小时前
前端基建从0到1搭建步骤清单(含工具选型+配置要点+落地注意事项)
前端·javascript·typescript·设计规范·前端工程化·规范化·前端基建
Hi~晴天大圣20 小时前
if 变量和if not 变量用法之布尔真假值规则
python·if
老歌老听老掉牙20 小时前
Vericut 5轴机床MCH文件解析与构建指南
xml·python·vericut
小白阿龙20 小时前
脚手架启动失败(Vue CLI/Vite/Create React App)
前端·vue.js·react.js
010米粉01020 小时前
Qt Cmake之路(一):Cmake变量语法
开发语言·qt·cmake
鞋尖的灰尘20 小时前
sdkman安装使用
前端
aini_lovee20 小时前
基于Jousselme距离改进D-S证据理论matlab实现
开发语言·算法·matlab
Aniugel20 小时前
Vue国际化实现多语言方案
前端·vue.js·面试
颜*鸣&空20 小时前
Qt Creator快速搭建项目
开发语言·qt