Python 将土味情话语录设置为桌面壁纸
时间:2022-07-25
本文章向大家介绍Python 将土味情话语录设置为桌面壁纸,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
每天学一句,单身远离我!
import 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)
- 区块链时代即将到来 它将如何改变我们的工作和生活
- 架设ftp反向代理服务器
- CentOS 7 上面安装PowerShell
- 苹果刻意降低旧 iPhone 效能的做法,或在法国面临刑事检控
- maven常用插件: 打包源码 / 跳过测试 / 单独打包依赖项
- 通过SmtpClient发送Exchange会议邮件
- Microsoft training Kits
- C#与yaml解析
- WordPress中的jQuery库不起作用的相关问题
- 人工智能时代家长不必做“虎妈”
- WCF 消息帧格式
- C#以post方式调用struts rest-plugin service的问题
- Web前端开发初级阶段需要学习的知识有哪些?
- 作为TensorFlow的底层语言,你会用C+构建深度神经网络吗?
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Windows平台PHP+IECapt实现网页批量截图并创建缩略图功能详解
- laravel 查询数据库获取结果实现判断是否为空
- 浅析PHP中的 inet_pton 网络函数
- php解压缩zip和rar压缩包文件的方法
- laradock环境docker-compose操作详解
- laravel中的fillable和guarded属性详解
- PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
- Laravel 6 将新增为指定队列任务设置中间件的功能
- Python生成器传参数及返回值原理解析
- PHP Swoole异步MySQL客户端实现方法示例
- PHP实现微信公众号验证Token的示例代码
- Laravel框架之解决前端显示图片问题
- thinkPHP5.1框架中Request类四种调用方式示例
- Python TestSuite生成测试报告过程解析
- PHP goto语句用法实例