代码可以直接使用的(所需的包都存在的情况下 python v 3.6+ )
注意代码中的路径信息
# 给图片添加水印并设置为桌面壁纸import PILimport timeimport osimport ctypesfrom PIL import Imagefrom PIL import ImageDrawfrom PIL import ImageFontdef background():print('开始设置桌面背景 \n')# 设置字体 字号useFont = ImageFont.truetype('C:\\Windows\\Fonts\\FZSTK.TTF', 34);# 引入同文件夹图片 当前目录下要有这张图片imagePath = 'background_back.jpg';background = Image.open(imagePath);# 获取当前日期 时间timestamp = time.localtime(time.time())week = u'星期日 星期一 星期二 星期三 星期四 星期五 星期六'.split()nowWeek = week[int(time.strftime('%w', timestamp))]nowTime = time.strftime('%Y-%m-%d', timestamp)day = time.strftime('%j', timestamp)year = int(time.strftime('%Y', timestamp))# 今年还剩的天数surplus = str(355 - int(day));if (year % 4 == 0):surplus = str(366 - int(day))# 水印内容content = nowTime + '\n' + nowWeek + '\n' + '今年还剩 ' + surplus + ' 天\n'# 绘画水印draw = ImageDraw.Draw(background)draw.text((460,70), content, '#fff', font = useFont)draw = ImageDraw.Draw(background)# 保存的文件名saveName = 'background.jpg'# 图片另存background.save(saveName)# 获取当前路径path = os.getcwd()# 获取要更换的图片路径filePath = path + '\\' + saveName# 设置桌面壁纸ctypes.windll.user32.SystemParametersInfoW(20, 0, filePath, 0)print('桌面背景设置成功')background()