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

相关推荐
SmartRadio7 小时前
CH585M+MK8000、DW1000 (UWB)+W25Q16的低功耗室内定位设计
c语言·开发语言·uwb
rfidunion8 小时前
QT5.7.0编译移植
开发语言·qt
少林码僧8 小时前
2.31 机器学习神器项目实战:如何在真实项目中应用XGBoost等算法
人工智能·python·算法·机器学习·ai·数据挖掘
rit84324998 小时前
MATLAB对组合巴克码抗干扰仿真的实现方案
开发语言·matlab
智航GIS8 小时前
10.4 Selenium:Web 自动化测试框架
前端·python·selenium·测试工具
jarreyer8 小时前
摄像头相关记录
python
宝贝儿好8 小时前
【强化学习】第六章:无模型控制:在轨MC控制、在轨时序差分学习(Sarsa)、离轨学习(Q-learning)
人工智能·python·深度学习·学习·机器学习·机器人
前端工作日常8 小时前
我学习到的A2UI概念
前端
大、男人8 小时前
python之asynccontextmanager学习
开发语言·python·学习
hqwest8 小时前
码上通QT实战08--导航按钮切换界面
开发语言·qt·slot·信号与槽·connect·signals·emit