关于使用python给图片打水印的方法

完整代码

'''
安装模块:filestools
安装方法: pip install filestools
函数名称:add_mark,用于实现添加水印。
file:原始照片;
mark:使用哪些字符作为水印;
out:添加水印后保存的位置;
color:水印字体的颜色,默认颜色#8B8B1B;
size:水印字体的大小,默认50;
opacity:水印字体的透明度,默认0.15;
space:水印字体之间的间隔, 默认75个空格;
angle:水印字体的旋转角度,默认30度;
'''
import os
import time

from watermarker.marker import add_mark

image_types = [".jpg", ".jpeg", ".png"]
path_old = input("请输入即将转换的图片所在目录:")
path_new = input("请输入转换完成后的存储目录:")
watermark = input("请输入水印内容:")
opacity = input("请输入字体透明度(默认在0.15,范围在0-1之间):")
if opacity:
    opacity = float(opacity)
    if opacity > 1.0:
        opacity = 1.0
    if opacity < 0:
        opacity = 0
else:
    opacity = 0.15
angle = input("请输入字体旋转角度(默认在30,范围0-180):")
if angle:
    angle = int(angle)
    if angle > 180:
        angle = 180
    if angle < 0:
        angle = 0
else:
    angle = 30
space = input("请输入字体之间的间隔(默认在75,范围0-200):")
if space:
    space = int(space)
    if space > 200:
        space = 200
    if space < 0:
        space = 0
else:
    space = 75
color = input("请输入水印颜色,请使用16进制数值(默认值:#8B8B1B):")
if not color:
    color = "#8B8B1B"

print(f"\n\n当前参数,输入路径:{path_old},输出路径:{path_new},\n水印内容:{watermark},透明度:{opacity},\n旋转角度:{angle},字体间隔:{space},字体颜色:{color}\n\n")

for root, dirs, files in os.walk(path_old):
    for file in files:
        file_arr = os.path.splitext(file)
        if file_arr[1] in image_types:
            image_path_old = os.path.join(path_old, file)
            add_mark(file = image_path_old, out=path_new, mark=watermark, opacity=opacity, angle=angle, space=space, color=color)

time.sleep(10)

posted @ 2023-05-19 09:40  影梦无痕  阅读(187)  评论(0编辑  收藏  举报