python土味情话_Python 将土味情话语录设置为桌面壁纸
本文編寫(xiě)于 128 天前,最后修改于 128 天前,其中某些信息可能已經(jīng)過(guò)時(shí)。
41041-3yfokd0irbe.png
38220-tlrmwji3zwo.pngimport os
import tempfile
import time
import requests
import win32api
import win32con
import win32gui
from PIL import Image, ImageDraw, ImageFont
import random
def setWallPaper(pic):
key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(key, "WallpaperStyle", 0, win32con.REG_SZ, "2")# 2拉伸適應(yīng)桌面,0桌面居中
win32api.RegSetValueEx(key, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, pic, 1 + 2)
print('成功應(yīng)用桌面壁紙')
def love():
url = r'https://api.vvhan.com/api/love'
res = requests.get(url).text
content = "『{}』".format(res)
return content
def love2():
url = r'https://api.lovelive.tools/api/SweetNothings/WebSite'
res = requests.get(url).text
content = "『{}』".format(res)
return content
def hitokoto():
url = 'https://v1.hitokoto.cn/'
res = requests.get(url).json()
hitokoto = res['hitokoto'].strip('。')
from_where = res['from']
from_who = res['from_who']
content = "『{}』".format(hitokoto)
return content
def word():
global word_type
if word_type == '0':
return hitokoto()
elif word_type == '1':
return random.choice([love(), love2()])
def jpg(path):
if not os.path.exists(path):
url = 'https://cn-south-227-storage-hitokoto-19627663.oss.dogecdn.com/pic/qf3cu.jpg'
content = requests.get(url).content
with open(path, 'wb+') as f:
f.write(content)
# 語(yǔ)錄類(lèi)型
word_type = 0
# 臨時(shí)目錄
root_path = tempfile.gettempdir()
# 背景圖片路徑
bgfile = os.path.join(root_path, "bg.jpg")
# 最終的壁紙圖片路徑
wallfile = os.path.join(root_path, "wall.jpg")
def main():
# 下載背景圖
jpg(bgfile)
# 打開(kāi)背景圖
img = Image.open(bgfile)
# 創(chuàng)建空白圖
d = ImageDraw.Draw(img)
# 設(shè)置字體
font = ImageFont.truetype("simhei.ttf", 60, encoding="utf-8")
width, height = img.size
length = 99
one_word = ""
# 確保語(yǔ)錄不超過(guò)壁紙寬度
while (length > width//60):
# 獲取語(yǔ)錄
one_word = word()
length = len(one_word)
time.sleep(1)
print(one_word)
# 語(yǔ)錄添加到圖片
d.text((width / 2 - 30 * (len(one_word)), height / 2 - 240), one_word, font=font, fill=(80, 80, 80, 128))
# 報(bào)錯(cuò)圖片
img.save(wallfile)
# 關(guān)閉流
img.close()
# 設(shè)置壁紙
setWallPaper(wallfile)
# 刪除壁紙圖片
os.unlink(wallfile)
# 刪除背景圖片
# os.unlink(bgfile)
if __name__ == '__main__':
word_type = input('>> 類(lèi)型(0:一言; 1:情話):').strip()
while True:
main()
time.sleep(5)
總結(jié)
以上是生活随笔為你收集整理的python土味情话_Python 将土味情话语录设置为桌面壁纸的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux中央服务器,如何在Linux上
- 下一篇: ROCKOUT软件测试工程师,具透丨这才