使用Midjourney-api批量生成图片alt

识别结果

识别结果 之前做了个网站用来放一些拍的照片,数量有点多,后面想到用MJ来生成图片的alt,顺带优化下SEO,但是准确率感人,不过能懒则懒了

示例脚本

import base64
import time

import requests


def get_base64(img_path):
    with open(img_path, 'rb') as img_file:
        img_bytes = img_file.read()

    img_b64 = base64.b64encode(img_bytes).decode('utf-8')
    img_b64 = 'data:image/jpeg;base64,' + img_b64
    return img_b64


def get_photo_desc(image_path):
    domain = "https://xxx.zeabur.app"
    headers = {
        "mj-api-secret": "your mj api secret",
        "Content-Type": "application/json"
    }
    data = {
        "base64": get_base64(image_path),
        "notifyHook": "",
        "state": ""
    }
    
    # 提交图片
    response = requests.post(f"{domain}/mj/submit/describe", json=data, headers=headers).json()
    # {"code":1,"description":"提交成功","result":"1690962880596501","properties":{}}
    result_id = response["result"]
    url = f"{domain}/mj/task/{result_id}/fetch"

    response = requests.get(url, headers=headers).json()
    
    # 这边轮询获取结果
    while response["status"] != "SUCCESS":
        time.sleep(2)
        response = requests.get(url, headers=headers).json()
    prompt = response["prompt"]
    prompts = prompt.split("\n\n")
    target = prompts[0][4:]
    # 部分输出的desc会带有http链接,在这里做一层判断
    for pp in prompts:
        if "http" not in pp: 
            target = pp[4:-9].split(",")[0]
            break
    # 可选翻译接口,这边的translate是用腾讯云的翻译接口实现的
    # 由于网站是支持多语言的,所以图片的alt是存在数据库里的,直接就翻译后形成多语言的alt了。
    alt = translate(target, source='en')

API提交后discord的返回结果

API提交后discord的返回结果.png

依赖

midjourney-proxy

可以选择合适的部署平台,我这边以zeabur为例