python土味情话_Python 将土味情话语录设置为桌面壁纸
本文編寫于 128 天前,最后修改于 128 天前,其中某些信息可能已經過時。
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拉伸適應桌面,0桌面居中
win32api.RegSetValueEx(key, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, pic, 1 + 2)
print('成功應用桌面壁紙')
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)
# 語錄類型
word_type = 0
# 臨時目錄
root_path = tempfile.gettempdir()
# 背景圖片路徑
bgfile = os.path.join(root_path, "bg.jpg")
# 最終的壁紙圖片路徑
wallfile = os.path.join(root_path, "wall.jpg")
def main():
# 下載背景圖
jpg(bgfile)
# 打開背景圖
img = Image.open(bgfile)
# 創建空白圖
d = ImageDraw.Draw(img)
# 設置字體
font = ImageFont.truetype("simhei.ttf", 60, encoding="utf-8")
width, height = img.size
length = 99
one_word = ""
# 確保語錄不超過壁紙寬度
while (length > width//60):
# 獲取語錄
one_word = word()
length = len(one_word)
time.sleep(1)
print(one_word)
# 語錄添加到圖片
d.text((width / 2 - 30 * (len(one_word)), height / 2 - 240), one_word, font=font, fill=(80, 80, 80, 128))
# 報錯圖片
img.save(wallfile)
# 關閉流
img.close()
# 設置壁紙
setWallPaper(wallfile)
# 刪除壁紙圖片
os.unlink(wallfile)
# 刪除背景圖片
# os.unlink(bgfile)
if __name__ == '__main__':
word_type = input('>> 類型(0:一言; 1:情話):').strip()
while True:
main()
time.sleep(5)
總結
以上是生活随笔為你收集整理的python土味情话_Python 将土味情话语录设置为桌面壁纸的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux中央服务器,如何在Linux上
- 下一篇: python转义引号的作用_python