import requests
import json
import os
1. 配置 Stable Diffusion WebUI 的 API 地址
API_URL = "http://127.0.0.1:7860/sdapi/v1/txt2img"
2. 定义核心 Prompt (提示词) - 这里融入了我们的潮汕美食元素
base_prompt = """
(masterpiece, best quality, 3d render, c4d style),
cute blocky cat character wearing denim hat and sunglasses,
eating Puning rice noodles (普宁粿条),
steaming hot food, cinematic lighting, depth of field,
blurred background of traditional Chaoshan street.
"""
negative_prompt = "low quality, blurry, deformed hands, extra limbs"
3. 设置生成参数 (模拟 ComfyUI/SDXL 的设置)
payload = {
"prompt": base_prompt,
"negative_prompt": negative_prompt,
"steps": 30, # 步数
"width": 1024, # SDXL 推荐分辨率
"height": 1024,
"cfg_scale": 7,
"sampler_name": "DPM++ 2M Karras",
"batch_size": 4 # 一次生成4张,提高效率
}
4. 发送请求并保存图片
def generate_ip_art():
print("🚀 正在启动 AI 绘图引擎...")
try:
response = requests.post(url=API_URL, json=payload)
response.raise_for_status()
r = response.json()
创建保存目录
if not os.path.exists('output_ip'):
os.makedirs('output_ip')
遍历返回的图片数据并保存
for i, img_str in enumerate(r'images'):
import base64
image_data = base64.b64decode(img_str)
file_path = f"output_ip/puning_cat_{i+1}.png"
with open(file_path, 'wb') as f:
f.write(image_data)
print(f"✅ 成功生成并保存: {file_path}")
except Exception as e:
print(f"❌ 生成失败: {e}")
if name == "main":
generate_ip_art()

