将天气情况设置为桌面壁纸
发布时间:2020-12-17 17:25:35 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/env python# -*- coding: utf-8 -*-##-------------------------------------------------------------------------------# Name: 解析天
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #!/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)) 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 PanelDesktop",win32con.KEY_SET_VALUE) win32api.RegSetValueEx(k,"WallpaperStyle",win32con.REG_SZ,styleSetting) win32api.RegSetValueEx(k,"TileWallpaper",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,B)) bgImage.save(CWD + os.sep + "backgroud.bmp","BMP") setWallpaperFromBMP(CWD + os.sep + "backgroud.bmp") bgImage.close() 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |