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

相关推荐
island1314几秒前
CANN ops-nn 算子库深度解析:核心算子(如激活函数、归一化)的数值精度控制与内存高效实现
开发语言·人工智能·神经网络
木斯佳2 分钟前
前端八股文面经大全:26届秋招滴滴校招前端一面面经-事件循环题解析
前端·状态模式
xcLeigh10 分钟前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh10 分钟前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
光影少年22 分钟前
react状态管理都有哪些及优缺点和应用场景
前端·react.js·前端框架
YongCheng_Liang30 分钟前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)
python·自动化·运维开发
鸽芷咕41 分钟前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?
python·microsoft·自动化·cann
秋邱42 分钟前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南
开发语言·c++·python
wenzhangli71 小时前
ooderA2UI BridgeCode 深度解析:从设计原理到 Trae Solo Skill 实践
java·开发语言·人工智能·开源
灵感菇_1 小时前
Java 锁机制全面解析
java·开发语言