博 客 - 正文

AIGC-真人转动漫 代码示例

来源:DatafusionAPI-数聚 分类:AIGC datafusionapi666 阅读(130)

import requests
from loguru import logger
import base64
import json
import time
key_ = ''
def add_task(image_path):
    with open(image_path, "rb") as image_file:
        # 将图片转换为 base64 编码
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
    url = 'https://www.datafusionapi.com/api/aigc/real2carton?key='+key_
    data = {
        'base64_image': f"data:image/jpeg;base64,{encoded_string}",
        'style_id': '0'
    }
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
    }
    resp = requests.post(url, json=data)
    logger.debug(resp.text)
    file_name = resp.json()['data']
    return file_name

def fetch_file(file_list):
    api_url = 'https://www.datafusionapi.com/api/common/fetch_file_from_server?key='+key_
    data = {
        'file_list': file_list
    }
    resp = requests.post(api_url, json=data)
    logger.debug(resp.text)
    resp = json.loads(resp.text)
    image_list = resp['data']
    logger.info(image_list)
    return json.loads(image_list)

if __name__ == '__main__':
    file_list = []
    file_list.append(add_task('aa42db1a656b9f13d54268e06176d09c.png'))

    # image_list = fetch_file(file_list)
   
    while(True):
        image_list = fetch_file(file_list)
        if len(image_list) == 0:
            # 控制返回图片到频率,可以批量获取返回文件,最多一次返回10个
            time.sleep(3)
            continue
        else:
            for image_ in image_list:
                # 出现url 链接便是生成好了,可以下载使用,只有半小时有效期
                logger.info(image_)
            break


数据驱动未来

立即注册
在线咨询
点击咨询

工作时间:8:00-24:00

返回顶部