博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python土味情话_Python 将土味情话语录设置为桌面壁纸
阅读量:1542 次
发布时间:2019-04-21

本文共 2320 字,大约阅读时间需要 7 分钟。

本文编写于 128 天前,最后修改于 128 天前,其中某些信息可能已经过时。

d61494076171123f3c579affb3e641ee.gif41041-3yfokd0irbe.png

d61494076171123f3c579affb3e641ee.gif38220-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)

转载地址:http://eordy.baihongyu.com/

你可能感兴趣的文章
【python】如何用python生成并保存gif动态图
查看>>
【机器学习】核函数的理解与常见核函数
查看>>
【一次认识一个市场技术指标】之ATR(真实波幅指标)
查看>>
【一次认识一个市场技术指标】之RSI(相对强弱指标)
查看>>
【一次认识一个市场技术指标】之KDJ(随机指标)
查看>>
一文读懂量化系统接入及相关平台
查看>>
【vn.py】 策略实盘自动交易
查看>>
【机器学习】多元函数梯度的理解
查看>>
【金融量化】期货中的对手价、市价、排队价、最新价分别表示什么价位
查看>>
【矩阵论】《理解矩阵》学习笔记
查看>>
【机器学习】网络表征学习、网络嵌入必读论文
查看>>
【机器学习】极大似然估计、最大后验估计的对比与联系
查看>>
【机器学习】模糊信息粒化(Fuzzy Information Granulation, FIG)
查看>>
【机器学习】模糊认知图(Fuzzy Cognitive Map, FCM)概念介绍
查看>>
【机器学习】L1、L2正则化项的理解及其在机器学习中的应用
查看>>
什么是P问题、NP问题和NPC问题
查看>>
【机器学习】最小二乘法的理解
查看>>
Python数据可视化之使用GridSpec自定义子图
查看>>
使用sklearn进行对数据标准化、归一化以及将数据还原
查看>>
Tensorflow中关于参数初始化的方法
查看>>