将天气情况设置为桌面壁纸

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#-------------------------------------------------------------------------------
# Name: 解析天气数据类
#
# Author: Small White
#
# Created: 2014-09-09
#
# Python Tested: 3.4.1
#
# dependency:
# 1) Pillow
# 2) pywin32 (e.g. Python for Windows)
# 3) win32-py3.4
#
# Modification History:
#-------------------------------------------------------------------------------

from PIL import Image, ImageDraw, ImageEnhance
import win32gui,win32con,win32api
import os

from WeatherReport34 import WeatherReport

CWD = os.path.split(os.path.realpath(__file__))[0]

def reduceOpacity(image, opacity):
        """Returns an image with reduced opacity."""
        assert opacity >= 0 and opacity <= 1
        if image.mode != 'RGBA':
            image = image.convert('RGBA')
        alpha = image.split()[3]
        alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
        image.putalpha(alpha)
        return image

def addWatermark(image, watermark, position, opacity=1):
    """Adds a addWatermark to an image."""
    if opacity < 1:
        watermark = reduceOpacity(watermark, opacity)
    if image.mode != 'RGBA':
        image = image.convert('RGBA')
    # create a transparent layer the size of the image and draw the
    # watermark in that layer.
    layer = Image.new('RGBA', image.size, (0,0,0,0))
    if position == 'tile':
        for y in range(0, image.size[1], watermark.size[1]):
            for x in range(0, image.size[0], watermark.size[0]):
                layer.paste(watermark, (x, y))
    elif position == 'scale':
        # scale, but preserve the aspect ratio
        ratio = min(
            float(image.size[0]) / watermark.size[0], float(image.size[1]) / watermark.size[1])
        w = int(watermark.size[0] * ratio)
        h = int(watermark.size[1] * ratio)
        watermark = watermark.resize((w, h))
        layer.paste(watermark, ((image.size[0] - w) / 2, (image.size[1] - h) / 2))
    else:
        layer.paste(watermark, position)
    # composite the watermark with the layer
    return Image.composite(layer, image, layer)
    
def setWallpaperFromBMP(imagepath, paperStyle = "TILE"):
    """
    Chage desktop background using the picture from the imagepath
    """
    # 平铺 (默认)
    styleSetting = "1"
    tileSetting = "1"
    if paperStyle == "CENTER":# 0桌面居中
        styleSetting = "0"
        tileSetting = "0"
    elif paperStyle == "STRETCH":# 2拉伸适应桌面
        styleSetting = "2"
        tileSetting = "0"
        
    k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
    win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, styleSetting)
    win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, tileSetting)
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)

def hasCrossLineBetweenBoxes(box1, box2):
    return abs((box1[0]+box1[2])/2.0 - (box2[0]+box2[2])/2.0) <= ((box1[2]+box2[2]-box1[0]-box2[0])/2.0) and \
           abs((box1[1]+box1[3])/2.0 - (box2[1]+box2[3])/2.0) <= ((box1[3]+box2[3]-box1[1]-box2[1])/2.0)

if __name__ == "__main__":
    screenWidth = vscreenwidth = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
    screenHeight = vscreenheigth = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
    vscreenx = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
    vscreeny = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
    
    '''
    Returns only one monitor size, so we don't use them.
    screenWidth = win32api.GetSystemMetrics(win32con.SM_CXSCREEN)
    screenHeight = win32api.GetSystemMetrics(win32con.SM_CYSCREEN)
    '''
    
    bgImage = Image.new('RGBA', (screenWidth, screenHeight), color = 0)
    bgImageDraw = ImageDraw.ImageDraw(bgImage)

    weatherReport = WeatherReport()
    weatherReportImage = weatherReport.getWeatherReportImage()
    
    bgImage = addWatermark(bgImage, weatherReportImage, (15,15), 1)
    weatherReportImage.close()
   
    if bgImage.mode != 'RGB':
        R,G,B,A = bgImage.split()
        bgImage = Image.merge("RGB",(R,G,B))
    
    bgImage.save(CWD + os.sep + "backgroud.bmp","BMP")

    setWallpaperFromBMP(CWD + os.sep + "backgroud.bmp")
    bgImage.close()