from PIL import Image, ImageDraw, ImageFont
import requests
from io import BytesIO

WIDTH = 600
HEIGHT = 400

# 全新专用天气实景图链接（无手机、纯天气画面）
WEATHER_IMG_URL = {
    "sun": "https://images.unsplash.com/photo-1592210454359-9043f067919b?w=600&h=400&fit=crop",
    "cloudy": "https://images.unsplash.com/photo-1534088568597-a689d38df448?w=600&h=400&fit=crop",
    "overcast": "https://images.unsplash.com/photo-1541673565319-88f0bf9c4728?w=600&h=400&fit=crop",
    "rain": "https://images.unsplash.com/photo-1515694346937-9dbb0942d36c?w=600&h=400&fit=crop",
    "snow": "https://images.unsplash.com/photo-1491002052546-bf38f186af56?w=600&h=400&fit=crop",
    "fog": "https://images.unsplash.com/photo-1543325216-4f98a471829b?w=600&h=400&fit=crop",
    "thunder": "https://images.unsplash.com/photo-1534088568597-a689d38df448?w=600&h=400&fit=crop",
    "wind": "https://images.unsplash.com/photo-1507400492013-162706c8c05e?w=600&h=400&fit=crop"
}

WEATHER_NAME = {
    "thunder": "雷阵雨",
    "sun": "晴天",
    "cloudy": "多云",
    "overcast": "阴天",
    "rain": "小雨",
    "snow": "下雪",
    "fog": "大雾",
    "thunder": "雷阵雨",
    "wind": "大风"
}

def get_background_img(weather_type):
    try:
        url = WEATHER_IMG_URL[weather_type]
        resp = requests.get(url, timeout=10)
        resp.raise_for_status()
        img = Image.open(BytesIO(resp.content))
        img = img.resize((WIDTH, HEIGHT), Image.Resampling.LANCZOS)
        return img
    except Exception:
        # 网络异常兜底纯色背景
        color_map = {
            "sun": (135, 206, 235),
            "cloudy": (150, 210, 240),
            "overcast": (120, 140, 160),
            "rain": (90, 130, 170),
            "snow": (180, 200, 220),
            "fog": (200, 205, 210),
            "thunder": (50, 70, 90),
            "wind": (130, 185, 230)
        }
        return Image.new("RGB", (WIDTH, HEIGHT), color_map.get(weather_type, (255,255,255)))

def draw_real_weather(weather_type, save_path="real_weather.png"):
    if weather_type not in WEATHER_IMG_URL:
        print("参数错误！可选：sun/cloudy/overcast/rain/snow/fog/thunder/wind")
        return

    img = get_background_img(weather_type)
    draw = ImageDraw.Draw(img)
    text = WEATHER_NAME[weather_type]

    # 字体兼容
    try:
        font = ImageFont.truetype("simhei.ttf", 36)
    except:
        font = ImageFont.load_default()

    # 白色文字更醒目
    draw.text((20, 20), text, fill=(255, 255, 255), font=font)

    img.save(save_path)
    print(f"图片已保存：{save_path}")
    img.show()

# 测试运行
if __name__ == "__main__":
    draw_real_weather("sun")