Windows下自動(dòng)編輯桌面壁紙

昨天我突發(fā)奇想,要寫(xiě)一個(gè)程序,每天給桌面壁紙?zhí)砑右粡?xkcd 的漫畫(huà)。于是我做了以下一系列事情,成功實(shí)現(xiàn)了功能。
注意:執(zhí)行前建議備份文件

找到桌面壁紙位置

我的 Windows 10 桌面壁紙是在C:\Users\[用戶名]\AppData\Roaming\Microsoft\Windows\Themes文件夾下的名為TranscodedWallpaper的文件。

使用爬蟲(chóng)獲取 xkcd 漫畫(huà)

xkcd 網(wǎng)站的結(jié)構(gòu)比較簡(jiǎn)單,只需要在 HTML 中找出 comic 圖像鏈接即可。使用代碼如下:

def get_xkcd_image():
    url = 'https://xkcd.com/'

    try:
        # 使用 requests 庫(kù)獲取網(wǎng)頁(yè)
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = r.apparent_encoding

        # 使用 bs4.BeautifulSoup 解析 HTML
        soup = BeautifulSoup(r.text, 'lxml')
        imgs = soup.find_all('img')

        # 在各個(gè)圖片鏈接中找到 xkcd 漫畫(huà)鏈接
        for i in imgs:
            src = str(i.get('src'))
            if '/comics/' in src:
                if src.startswith('//'):
                    src = 'https:' + src

                # 獲取并保存漫畫(huà)圖片
                imgfile = requests.get(src)
                imgfile.raise_for_status()
                with open('./resources/xkcd.jpg', 'wb') as f:
                    f.write(imgfile.content)
                    f.close()
                print('Get xkcd image success')
                break
        else:
            raise Exception(f'No comic found in {url}')

    # 出現(xiàn)異常時(shí)保存異常信息
    except Exception as e:
        print('Get xkcd image fail')
        with open(f'crash.log', 'a') as f:
            # current_date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            print(f'[{current_date}]:', e, file=f)
            f.close()

使用 PIL 圖像處理庫(kù)編輯壁紙

由于 PIL 庫(kù)不支持保存到無(wú)擴(kuò)展名文件,所以這里使用了復(fù)制文件的方法。

def write_to_wallpaper():
    try:
        # Open background
        wallpaper = Image.open('./resources/background.jpg')

        # Open xkcd image
        xkcd = Image.open('./resources/xkcd.jpg')
        wallpaper_xkcd = xkcd.resize((2 * xkcd.width, 2 * xkcd.height)).convert('RGB')
        # 我這里設(shè)置將 xkcd 漫畫(huà)放到壁紙右上角
        wallpaper.paste(wallpaper_xkcd, box=(wallpaper.width * 15 // 16 - wallpaper_xkcd.width, wallpaper.height // 16))

        # Save to wallpaper
        with open('./wallpaper.jpg', 'wb') as f:
            wallpaper.save(f)
            f.close()
        # username 需要換成自己的
        shutil.copyfile('./wallpaper.jpg', f'C:\\Users\\{username}\\AppData\\Roaming\\Microsoft\\Windows\\Themes\\TranscodedWallpaper')
        print('Write to wallpaper success')

    except Exception as e:
        print('Write to wallpaper fail')
        with open(f'crash.log', 'a') as f:
            print(f'[{current_date}]:', e, file=f)
            f.close()

預(yù)覽效果如下圖,可以看到壁紙右上角添加了一張 xkcd 漫畫(huà)。


wallpaper.jpg

將腳本添加到 Windows 任務(wù)

程序設(shè)置完成后,要想定時(shí)執(zhí)行,還需要添加到 Windows 任務(wù)。
使用 Windows 的搜索功能(快捷鍵:Windows+Q)找到“任務(wù)計(jì)劃程序”。點(diǎn)擊右側(cè)“操作”中的“創(chuàng)建任務(wù)”進(jìn)行設(shè)置。具體設(shè)置方法見(jiàn)參考資料中的“windows定時(shí)運(yùn)行exe文件”。


任務(wù)計(jì)劃程序

設(shè)置觸發(fā)器

一定要設(shè)置觸發(fā)器,否則任務(wù)不知道應(yīng)該在什么時(shí)間執(zhí)行。我設(shè)置為每天 23:00 執(zhí)行。


觸發(fā)器

設(shè)置操作時(shí)的參數(shù)

  • 程序或腳本:如果你的系統(tǒng)安裝了 python,“程序或腳本”一欄可以寫(xiě) python,也可以寫(xiě) python.exe 的路徑。
  • 添加參數(shù):“添加參數(shù)”只需要寫(xiě)腳本的路徑。(實(shí)際上可以都寫(xiě)到“程序或腳本”一欄,系統(tǒng)會(huì)自動(dòng)劃分參數(shù))
  • 起始于:一定要寫(xiě),否則程序可能不會(huì)正確執(zhí)行。這個(gè)參數(shù)設(shè)置為腳本的路徑。

這樣就實(shí)現(xiàn)了 Windows 下自動(dòng)編輯桌面壁紙。

參考資料

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容