将天气情况设置为桌面壁纸
发布时间:2020-05-24 23:40:34 所属栏目:Python 来源:互联网
导读:将天气情况设置为桌面壁纸
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 #!/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()
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
