该桌面背景有4个特点:
- 背景为一张从
必应下载的壁纸 - 英文为随机的名人名言,从
API获取 - 用百度翻译接口翻译英文名人名言
- 将桌面壁纸更换
效果:

源代码:
import requests
import json
import random
import hashlib
import urllib
import os
import time
from PIL import Image, ImageDraw, ImageFont
import ctypes
#百度翻译接口参数
appid = '' # 填写你的appid
secretKey = '' # 填写你的密钥
#创建壁纸图片文件夹
os.makedirs('./image/', exist_ok=True)
#bing壁纸接口
url = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10&mkt=zh-cn'
'''
翻译成中文
'''
def baidutrans(q):
myurl = '/api/trans/vip/translate'
fromLang = 'auto' # 原文语种
toLang = 'zh' # 译文语种
salt = random.randint(32768, 65536)
sign = appid + q + str(salt) + secretKey
sign = hashlib.md5(sign.encode()).hexdigest()
myurl ='https://api.fanyi.baidu.com'+ myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(salt) + '&sign=' + sign
response = requests.get(myurl)
jsonstrs = response.json()
return jsonstrs
"""
获取名人名言
"""
def favqs():
url='https://favqs.com/api/qotd'
response = requests.get(url)
jsonstrs = response.json()
# print(jsonstrs)
return jsonstrs
"""
更换背景图
"""
# def setWallPaper(pic):
# # 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, "2")
# win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
# # refresh screen
# win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)
if __name__ == "__main__":
response = requests.get(url)
# print(response.text)
# jsonstrs = json.loads(response.text) # json解析响应文本
jsonstrs = response.json()
# 或者jsonstr = response.json()
imgnum = 0
#exit()
for key in jsonstrs['images']:
# print(key['url'], end='\n')
# r = requests.get('https://www.bing.com'+key['url'])
url = 'https://www.bing.com'+key['url']
# print(requests.get(url, stream=True).raw.read())
image = Image.open(requests.get(url, stream=True).raw).convert('RGBA')
mrmyjson = favqs()
author = mrmyjson['quote']['author']
body = mrmyjson['quote']['body']
body=body+' ----'+author
# 设置字体样式
font_type = r'C:\Windows\Fonts\tahoma.ttf'
font_type2='STXINGKA.TTF'
font = ImageFont.truetype(font_type, 30)
font2 = ImageFont.truetype(font_type2, 31)
color = "#fff"
color2 = "#FFFF00"
draw = ImageDraw.Draw(image)
width, height = image.size
fw, fh = font.getsize(body)
bodyarr=body.split(' ')
fontfw=0
book_x = width / 2-height/2
if(book_x<=0):
book_x=width/4
book_y =height- height / 4
# 处理 英文单词换行
for num, book in enumerate(bodyarr):
book=book+' '
fw2, fh2 = font.getsize(book)
#print(u'%s:%s' % (book,fw2))
if(fontfw>(width-width/4-50)):
fontfw=0
book_y=book_y+30
draw.text((book_x + 1 + fontfw, book_y + 1), ' ' + book, 'black', font)
draw.text((book_x + fontfw, book_y),' '+book, color, font)
fontfw = fontfw + fw2
zhbody= baidutrans(body)
for keybody in zhbody['trans_result']:
#print(keybody['dst'])
summary=keybody['dst']
summary_x = book_x
summary_y = book_y+50
summary_line = 35
# 45个字一行 后续可以完善
summary_list = [summary[i:i + 45] for i in range(0, len(summary), 45)]
for num, summary in enumerate(summary_list):
y = summary_y + num * summary_line
#阴影
draw.text((summary_x + 1 + 10, y + 1), u'%s' % summary, 'black', font2)
#文字
draw.text((summary_x+10, y), u'%s' % summary, color, font2)
imgpath='./image/img%s.png' % imgnum
image.save(imgpath)
imgnum=imgnum+1
pic =os.path.join(os.path.abspath('.'),r'image\img0.png') # 写绝对路径
# setWallPaper(pic)
ctypes.windll.user32.SystemParametersInfoW(20, 0, pic, 0)
# 只处理一张壁纸
break
# 可保持所有壁纸
# with open('./image/img%s.png' % num, 'wb') as f:
# f.write(r.content)
# num = num+1
本文介绍了一个Python脚本,该脚本能够自动从Bing下载壁纸并使用API获取随机名人名言,通过百度翻译接口将其翻译成中文,最后将包含名言的壁纸设置为桌面背景。
1806

被折叠的 条评论
为什么被折叠?



