核心内容

1.利用PLI.ImageDraw处理图片

2.利用ctypes.windll.user32.SystemParametersInfoW更换桌面背景

Code

将自己的图片分别命名为original.png作为备份,放到rescources文件夹中
复制original.png到根目录,并重命名为new.png

import ctypes
import calendar
import re
import datetime
import math
import os
import sys
import random
from PIL import Image, ImageDraw, ImageFont

# 切换到文件当前路径
cwd = os.path.abspath(os.path.dirname(sys.argv[0]))
os.chdir(cwd)

# 获取年月日星期信息
today = datetime.date.today()
year, month, day, weekday = today.year, today.month, today.day, today.weekday() + 1

# 匹配日历信息
msg = calendar.month(year, month)
pattern = "(.*?[0-9]{4}\n.*?\n)(.*?\n$)"
result = re.findall(pattern, msg, re.S)
title, num = result[0][0], result[0][1]

# 将日历信息写到图片上
# 每月1日清空原内容换新图片
if day == 1:
    img = Image.open("rescources\\original.png")
else:
    img = Image.open("new.png")

# 创建画布对象
img_draw = ImageDraw.Draw(img)
# 设置字体
font1 = ImageFont.truetype("simhei.ttf", 45, encoding="unic")
# 将日历信息写到图片上
# 第一个括号里是x,y坐标,第二个括号里是rgb值 可以自行调整
img_draw.text((1440, 675), title, (255, 165, 79), font1)
img_draw.text((1440, 760), num, (0, 0, 0), font1)

# 判断本日是本月第几个星期几
first = datetime.date(year, month, 1).weekday()
row, col = math.ceil((day + first) / 7), weekday

font2 = ImageFont.truetype("simhei.ttf", 70, encoding="unic")  # 65
# 初始1430,760,一行+42,一列+68
# 根据row, col得到坐标
x = 1430 + (col - 1) * 69
y = 750 + (row - 1) * 42

# 在x, y位置画圆
# 设置圆的颜色为随机值
c1 = random.randint(0, 255)
c2 = random.randint(0, 255)
c3 = random.randint(0, 255)
img_draw.text((x, y), "〇", (c1, c2, c3), font2)

# 存储修改后的图片
img.save("new.png", "PNG")

# 更换桌面背景
path = cwd + "\\new.png"
ctypes.windll.user32.SystemParametersInfoW(20, 0, path, 0)

预览效果

已隐藏所有桌面图标

原版

修改后

下载链接

点我下载 密码1234