python实现动态壁纸_Python 实现macOS Catalina 动态壁纸定时设置
前言
很不幸,我的電腦只能裝 macOS High Sierra ,但是看他們的 Catalina 和 Mojave 的壁紙好炫酷,據(jù)說還可以根據(jù)日出時(shí)間切換壁紙和暗黑模式?!盡管條件限制,我還是想體驗(yàn)一下動(dòng)態(tài)壁紙。(想試試的戳這里,Github有相同的說明)本人剛學(xué)Python 2秒,大佬輕噴,Github也沒啥項(xiàng)目
Windows 也能用啦:戳這里
Here we go
實(shí)現(xiàn)方法
計(jì)算日出時(shí)間
需要一點(diǎn)天文學(xué)知識,具體請看代碼(文末有,百度來的)
定時(shí)任務(wù)
為了保持輕量化原則,使用自帶的 sched
開機(jī)自啟動(dòng)
macOS 很貼心的加入了啟動(dòng)項(xiàng)功能,不用鼓搗命令行
設(shè)置壁紙
這里不太好弄,windows 下的方法不少,mac 上可以使用 appscript 模塊(是在是沒有辦法輕量化了) from appscript import app, mactypes
def set_bg(path): #注意這里的 path 是相對于項(xiàng)目路徑的
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,再轉(zhuǎn)換為絕對路徑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 運(yùn)行環(huán)境
在終端執(zhí)行(當(dāng)然你也可以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
會(huì)要求輸入經(jīng)緯度用以計(jì)算日出日落時(shí)間(float 最好)
Ctrl + C?退出終端
將?start.sh?添加到開機(jī)啟動(dòng)項(xiàng)(方法在此)
配置說明
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?是之前一小時(shí),+01:00?是之后一小時(shí)
所以,
"SunSet": {
"-01:00": "BackgroundImage/evening.jpeg"
}
指的是在日落前1小時(shí),將壁紙換成BackgroundImage/evening.jpeg(相對于項(xiàng)目路徑)
config.json 同時(shí)也存儲 longitude 和 latitude
后話
看完這篇文章,要不再看看這篇關(guān)于黑蘋果的文章?
Github地址:
https://github.com/HelloWorldZTR/py-background
https://github.com/HelloWorldZTR/py-background-for-win
正在上傳…重新上傳取消
還會(huì)有windows 版的
計(jì)算日出日落時(shí)間的代碼
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}
總結(jié)
以上是生活随笔為你收集整理的python实现动态壁纸_Python 实现macOS Catalina 动态壁纸定时设置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【SENCHA TOUCH】页面动画跳转
- 下一篇: CTF 栅栏加密解密----python