python实现将图片添加水印并设置为桌面背景

代码可以直接使用的(所需的包都存在的情况下  python v 3.6+ )

注意代码中的路径信息

# 给图片添加水印并设置为桌面壁纸
import PIL
import time
import os
import ctypes
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


def 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()
发布了50 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_36999656/article/details/100514406