import requests
import json
import time
from PIL import Image
import numpy as np
import io

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer 3a3b982990c44fc5ba5428320d72330e",
    "content-type": "application/json"
}

prompt = """A single luxurious 3D circular Token coin/currency symbol icon on pure white background. The round coin has the word 'TOKEN' elegantly embossed in large bold capital letters across the center as the only main element. The color scheme is tech blue: deep navy blue base with electric cyan and bright sky blue glowing gradients, accented with subtle silver platinum highlights. Surrounding the 'TOKEN' text is a glowing circular ring of binary code (0s and 1s) and neural network patterns in cyan tones. The coin has microchip circuit lines etched on the rim, and a soft blue glowing aura. Style: 3D rendered, ultra-detailed, photorealistic, premium fintech/AI/tech icon design, single round coin object only, no other letters or symbols, isolated on pure white background only."""

payload = {
    "model": "gpt-image-2",
    "prompt": prompt,
    "size": "1024x1024"
}

print("正在调用 gpt-image-2 API 生成科技蓝色调的 Token 货币符号...")
response = requests.post(url, json=payload, headers=headers)
result = response.json()
print(f"响应状态: {response.status_code}")

if response.status_code == 200 and 'data' in result and len(result['data']) > 0:
    img_url = result['data'][0].get('url')
    print(f"图片URL: {img_url}")

    img_response = requests.get(img_url)
    if img_response.status_code == 200:
        img = Image.open(io.BytesIO(img_response.content)).convert('RGBA')
        print(f"原始图片尺寸: {img.size}, 模式: {img.mode}")

        data = np.array(img)
        threshold = 240
        r, g, b, a = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
        white_mask = (r > threshold) & (g > threshold) & (b > threshold)
        data[:,:,3] = np.where(white_mask, 0, 255)
        img_transparent = Image.fromarray(data, 'RGBA')

        timestamp = int(time.time())
        filename = f"token-coin-tech-blue-transparent-{timestamp}.png"
        filepath = f"/var/www/html/share/{filename}"
        img_transparent.save(filepath, 'PNG')

        print(f"\n✅ 透明PNG已保存: {filepath}")
        print(f"访问地址: http://43.156.179.74/share/{filename}")

        filename_white = f"token-coin-tech-blue-whitebg-{timestamp}.png"
        filepath_white = f"/var/www/html/share/{filename_white}"
        with open(filepath_white, 'wb') as f:
            f.write(img_response.content)
        print(f"原图（白底）: http://43.156.179.74/share/{filename_white}")
else:
    print(f"错误响应: {json.dumps(result, indent=2, ensure_ascii=False)[:1000]}")