前言
很不幸,我的电脑只能装 macOS High Sierra ,但是看他们的 Catalina 和 Mojave 的壁纸好炫酷,据说还可以根据日出时间切换壁纸和暗黑模式?!尽管条件限制,我还是想体验一下动态壁纸。(想试试的戳这里,Github有相同的说明)本人刚学Python 2秒,大佬轻喷,Github也没啥项目
Windows 也能用啦:戳这里
Here we go
实现方法
计算日出时间
需要一点天文学知识,具体请看代码(文末有,百度来的)
定时任务
为了保持轻量化原则,使用自带的 sched
开机自启动
macOS 很贴心的加入了启动项功能,不用鼓捣命令行
设置壁纸
这里不太好弄,windows 下的方法不少,mac 上可以使用 appscript 模块(是在是没有办法轻量化了) from appscript import app, mactypes
def set_bg(path): #注意这里的 path 是相对于项目路径的
app(‘Finder’).desktop_picture.set(mactypes.File(path)) # Windows version
import win32con, win32api, win32gui
import os
def set_bg(path):
path2 = ”
for c in path: #为了兼容上面的代码,采取相对路径path,再转换为绝对路径path2
if c==’/’:
path2+=’\\’
else:
path2+=c
# get file path
pic = os.getcwd()+’\\’+path2
# open register
regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,”Control Panel\\Desktop”,0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(regKey,”WallpaperStyle”, 0, win32con.REG_SZ, “0”)
win32api.RegSetValueEx(regKey, “TileWallpaper”, 0, win32con.REG_SZ, “0”)
# refresh screen
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, pic, win32con.SPIF_SENDWININICHANGE)
安装方法
Github 仓库地址(mac OS)
Github 仓库地址(Windows)
安装说明 (mac)
保证拥有 Python3 运行环境
在终端执行(当然你也可以pip3 install appscript && python3 Main.py)
git clone https://github.com/HelloWorldZTR/py-background.git
cd py-background && sudo chmod +x install.sh start.sh
./install.sh
会要求输入经纬度用以计算日出日落时间(float 最好)
Ctrl + C 退出终端
将 start.sh 添加到开机启动项(方法在此)
配置说明
config.json
{
“SunSet”: {
“-01:00”: “BackgroundImage/evening.jpeg”,
“+00:00”: “BackgroundImage/night.jpg”
},
“SunRise”: {
“-01:00”: “BackgroundImage/evening.jpeg”,
“+00:00”: “BackgroundImage/day.jpg”
}
}
SunSet / SunRise 是日落日出; -01:00 是之前一小时,+01:00 是之后一小时
所以,
“SunSet”: {
“-01:00”: “BackgroundImage/evening.jpeg”
}
指的是在日落前1小时,将壁纸换成BackgroundImage/evening.jpeg(相对于项目路径)
config.json 同时也存储 longitude 和 latitude
后话
看完这篇文章,要不再看看这篇关于黑苹果的文章?
Github地址:
https://github.com/HelloWorldZTR/py-background
https://github.com/HelloWorldZTR/py-background-for-win
还会有windows 版的
计算日出日落时间的代码
import math
def getsunrise(year, month, day, latitude, longitude):
zenith = 90.83333333
N1 = math.floor(275 * month / 9)
N2 = math.floor((month + 9) / 12)
N3 = (1 + math.floor((year – 4 * math.floor(year / 4) + 2) / 3))
dayOfYear = N1 – (N2 * N3) + day – 30
localOffset = math.floor(-1 * longitude * 24/360)
lngHour = longitude / 15
t = dayOfYear + ((6 – lngHour) / 24)
M = (0.9856 * t) – 3.289
L = M + (1.916 * math.sin(M * 3.1415926 / 180)) + \
(0.020 * math.sin(2 * M * 3.1415926 / 180)) + 282.634
L = L – 360
RA = (180/3.1415926) * math.atan(0.91764 * math.tan(L * 3.1415926 / 180))
Lquadrant = (math.floor(L/90)) * 90
RAquadrant = (math.floor(RA/90)) * 90
RA = RA + (Lquadrant – RAquadrant)
RA = RA / 15
sinDec = 0.39782 * math.sin(L * 3.1415926 / 180)
cosDec = math.cos(math.asin(sinDec))
cosH = (math.cos(zenith * 3.1415926 / 180) – (sinDec * math.sin(latitude *
3.1415926 / 180))) / (cosDec * math.cos(latitude * 3.1415926 / 180))
if (cosH < -1):
sunsetT = 0
return sunsetT
if (cosH > 1):
sunriseT = 0
return sunriseT
H = 360 – 180/3.1415926 * math.acos(cosH)
H = H / 15
T = H + RA – (0.06571 * t) – 6.622
UT = T – lngHour
sunriseT = UT – localOffset
return sunriseT
def getsunset(year, month, day, latitude, longitude):
zenith = 90.83333333
N1 = math.floor(275 * month / 9)
N2 = math.floor((month + 9) / 12)
N3 = (1 + math.floor((year – 4 * math.floor(year / 4) + 2) / 3))
dayOfYear = N1 – (N2 * N3) + day – 30
localOffset = math.floor(-1 * longitude * 24/360)
lngHour = longitude / 15
t = dayOfYear + ((6 – lngHour) / 24)
M = (0.9856 * t) – 3.289
L = M + (1.916 * math.sin(M * 3.1415926 / 180)) + \
(0.020 * math.sin(2 * M * 3.1415926 / 180)) + 282.634
L = L – 360
RA = (180/3.1415926) * math.atan(0.91764 * math.tan(L * 3.1415926 / 180))
Lquadrant = (math.floor(L/90)) * 90
RAquadrant = (math.floor(RA/90)) * 90
RA = RA + (Lquadrant – RAquadrant)
RA = RA / 15
sinDec = 0.39782 * math.sin(L * 3.1415926 / 180)
cosDec = math.cos(math.asin(sinDec))
cosH = (math.cos(zenith * 3.1415926 / 180) – (sinDec * math.sin(latitude *
3.1415926 / 180))) / (cosDec * math.cos(latitude * 3.1415926 / 180))
if (cosH < -1):
sunsetT = 0
return sunsetT
if (cosH > 1):
sunriseT = 0
return sunriseT
H = 180/3.1415926 * math.acos(cosH)
H = H / 15
T = H + RA – (0.06571 * t) – 6.622
UT = T – lngHour
sunsetT = UT – localOffset
return sunsetT
def format_time(timeT):
if timeT > 0:
h = int(timeT)
m = int((timeT-int(timeT))*60)
else:
h = 24+int(timeT)-1
m = int(math.fabs(timeT-int(timeT))*60)
return {‘hour’: h, ‘minute’: m}
