import threading
import requests
import json
import base64
import time
import random


def image_to_base64(file_path, mime_type=None):
    if mime_type is None:
        extension = file_path.split('.')[-1].lower()
        mime_types = {
            'jpg': 'image/jpeg',
            'jpeg': 'image/jpeg',
            'png': 'image/png',
            'gif': 'image/gif',
            'webp': 'image/webp',
            'bmp': 'image/bmp'
        }
        mime_type = mime_types.get(extension, 'application/octet-stream')
    
    # 读取文件内容并编码为Base64
    with open(file_path, 'rb') as image_file:
        encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
    
    # 组合成Data URI格式
    return f"data:{mime_type};base64,{encoded_string}"

if __name__ == '__main__':
    url = 'http://112.126.94.241:18018/change_cloth_base64'
    headers = {'Content-Type': 'application/json'}
    img64_human_str = image_to_base64("/home/xsl/code/ComfyUI/input/girl_full.jpg")
    data = {}
    data['human_img'] = img64_human_str

    img64_cloth_str = image_to_base64("/home/xsl/code/ComfyUI/input/cloth_short.jpg")
    data['cloth_img'] = img64_cloth_str

    data['output_format'] = "url"


    try:
        response = requests.post(url, headers=headers, json=data)
        print(response.text)
    except Exception as e:
        print("error")