背景

最近有一些图片需要增加水印,找了一圈也没看见比较好的工具,又不想用破解的PS,干脆自己做了一个GUI工具,有需要的同学自取

功能

  • 支持水印预览
  • 自定义水印文字内容
  • 支持行楷和微软雅黑两种字体
  • 支持自定义字号
  • 支持自定义水印颜色
  • 支持自定义水印间距
  • 支持自定义水印透明度
  • 支持自定义水印旋转角度
  • 支持批量给图片增加水印
  • 仅支持Windows平台(用到了windows上的字体)

预览

使用方式

方法一:运行Python脚本。

电脑上需要安装python3运行环境

# 安装依赖包
pip install pillow pysimplegui
# 将下面的源码保存为: image_watermark_tool.py
python image_watermark_tool.py

方法二:直接下载exe文件,双击运行即可。

链接: https://pan.baidu.com/s/1veoNlDJOecq7bw0rQB-FYg

提取码: b9pe

exe程序由下面的源码打包而成

pyinstaller -F -w image_watermark_tool.py

源码

# author zhenglisai
from PIL import Image, ImageDraw, ImageFont
import PySimpleGUI as sg
import os
from io import BytesIO
import base64
import traceback def add_watermark(image, msg, font, font_size, color, distance, transparency, angle):
if font == "xingkai":
font_name = "C:\\WINDOWS\\Fonts\\STXINGKA.TTF"
else:
font_name = "C:\\WINDOWS\\Fonts\\msyh.ttc"
font = ImageFont.truetype(font_name, int(font_size))
if color == "" or color == "None":
r = 0
g = 0
b = 0
else:
color = color.replace("#", "0x")
color = int(color, base=16) if isinstance(color, str) else color
r = (color >> 16) & 0xff
g = (color >> 8) & 0xff
b = color & 0xff
background = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (0, 0, 0, 0))
background.paste(image, image.size)
font_len = len(msg)
rgba_image = background.convert('RGBA')
text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(text_overlay)
for i in range(0, rgba_image.size[0], font_len * 40 + int(distance)):
for j in range(0, rgba_image.size[1], int(distance)):
image_draw.text((i, j), msg, font=font, fill=(r, g, b, int(transparency)))
text_overlay = text_overlay.rotate(int(angle))
image_result = Image.alpha_composite(rgba_image, text_overlay)
image_result = image_result.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
return image_result frame = [
[sg.Text("水印"), sg.InputText("郑立赛", key="text", enable_events=True)],
[sg.Text("字体"), sg.Radio("行楷", "font", default=True, key="xingkai", enable_events=True), sg.Radio("微软雅黑", "font", key="yahei", enable_events=True)],
[sg.Text("字号"), sg.Slider(range=(1, 100), default_value=36, key="font_size", orientation='horizontal', enable_events=True)],
[sg.Button("选择水印颜色", button_type=sg.BUTTON_TYPE_COLOR_CHOOSER, target="color"),sg.InputText("#000000", enable_events=True, key="color", readonly=True, size=(10, 10))],
[sg.Text("间距"), sg.Slider(range=(1, 500), default_value=200, key="distance", orientation='horizontal', enable_events=True)],
[sg.Text("透明度"), sg.Slider(range=(0, 255), default_value=50, key="transparency", orientation='horizontal', enable_events=True)],
[sg.Text("角度"), sg.Slider(range=(-90, 90), default_value=45, key="angle", orientation='horizontal', enable_events=True)],
[sg.Text("使用说明", text_color="red")],
[sg.Text("第一步:调整参数,预览水印")],
[sg.Text("第二部:点击下方选择图片")],
[sg.Button("打开图片", key="open_image")],
[sg.Text("处理进度"), sg.ProgressBar(100, size=(30, 10), orientation="h", key="progress")]
]
frame_image = [
[sg.Image(key="image"), sg.Sizer(640, 480)]
] layout = [
[sg.Frame(title="参数", layout=frame), sg.Frame(title="效果预览", layout=frame_image, element_justification="center")]
]
default_windows_size = (800, 480)
window = sg.Window("批量添加水印工具",
layout,
default_element_size=(40, 1),
grab_anywhere=True,
size=default_windows_size,
resizable=True,
finalize=True,
enable_close_attempted_event=True
) while True:
try:
event, value = window.read()
if event == sg.WIN_CLOSED:
break
if event == "open_image":
image_path_list = sg.popup_get_file("选择图片",
no_window=True,
multiple_files=True
)
source_image_path = os.path.sep.join(image_path_list[0].split("/")[:-1])
result_path = os.path.join(source_image_path, "watermark")
if not os.path.exists(result_path):
os.mkdir(result_path)
image_count = len(image_path_list)
text = value["text"]
font_size = value["font_size"]
distance = value["distance"]
transparency = value["transparency"]
angle = value["angle"]
color = value["color"]
progress_bar = window["progress"]
count = 1
total = len(image_path_list)
for image_path in image_path_list:
image_name = image_path.split("/")[-1].split(".")[0]
image_source = Image.open(image_path)
if value["yahei"]:
font = "yahei"
else:
font = "xingkai"
image_result = add_watermark(image_source, text, font, font_size, color, distance, transparency, angle)
image_result_path = os.path.join(result_path, "%s.png" % image_name)
image_result.save(image_result_path)
buffered = BytesIO()
image_result.save(buffered, format="PNG")
data = base64.b64encode(buffered.getvalue())
window["image"].update(data=data)
progress_bar.UpdateBar(current_count=count, max=total)
count += 1
sg.popup_ok('%s张水印图片已保存在: %s' % (image_count, result_path))
if event in ["angle", "transparency", "distance", "yahei", "xingkai", "font_size", "text", "color"]:
image_demo = Image.new('RGBA', (640, 480), (0, 0, 0, 0))
if value["yahei"]:
font = "yahei"
else:
font = "xingkai"
image_result = add_watermark(image_demo, value["text"], font, value["font_size"], value["color"], value["distance"], value["transparency"], value["angle"])
buffered = BytesIO()
image_result.save(buffered, format="PNG")
data = base64.b64encode(buffered.getvalue())
window["image"].update(data=data)
except:
sg.Print(traceback.format_exc())
window.close()

【Python】批量给图片增加水印工具的更多相关文章

  1. 如何用node.js批量给图片加水印

    上一篇我们讲了如何用node.js给图片加水印,但是只是给某一张图片加,并没有涉及到批量处理.这一篇,我们学习如果批量进行图片加水印处理. 一.准备工作: 首先,你要阅读完这篇文章:http://ww ...

  2. C#图片增加水印

    给图片增加水印 1.引用 using System.Drawing; 2.代码实现 string ImagePath = @"C:\Users\RAPOO\Pictures\Camera R ...

  3. Python批量修改图片格式和尺寸

    Python批量修改图片格式和尺寸 备注: 1.导入了PIL库,是处理图片用的,很强大; 2.导入了的win32库,是判断隐藏文件用的,我们的项目需要删除隐藏文件,不需要的可以直接找到删除. 3.导入 ...

  4. Python实现给图片加水印功能

    前言 最近忙得连轴转,很久没更新博客了,代码倒是没啥写,积累了好些东西,接下来一有时间就来更新吧~ 本文记录使用Python实现给图片添加水印的功能实现过程 先看效果 把公众号的封面作为素材 原图是这 ...

  5. Python 批量修改图片格式和尺寸

    公司的一个项目要求把所有4096x4096的图片全部转化成2048x2048的图片,这种批量转换图片大小的软件网上很多,我的同事原来使用的美图看看的批量转换,但是稍微有点麻烦,每次还需要指定要转换的图 ...

  6. tinypng的python批量压缩图片功能

    tinypng网站提供的图片压缩功能很不错,但是直接在网站上压缩有限制,大量压缩图片时比较麻烦,还好官方提供了很多脚本的自动化压缩接口.下面简单说下python批量压缩步骤. 1.申请api key ...

  7. python批量裁剪图片

    """用Pythonp批量裁剪图片""" from PIL import Imageimport matplotlib.pyplot as ...

  8. 【python】PIL 批量绘制图片矩形框工具

    工具采用PIL:Python Imaging Library,图像处理标准库.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux下直接通过apt安装 $ ...

  9. ps技术--批量给图片加水印

    在日常的办公过程中,对于一些比较重要的文件的扫描件需要特殊处理,这时我们就需要给它们加上水印,保证它们的用途唯一,而这些扫描件很多,不可能一一给他们加水印,所以为提高工作效率,我们就可以使用一些小软件 ...

随机推荐

  1. JavaScript03

    类型转换和运算符 typeof函数 检测数据类型,可以使用以下两种调用的方式: typeof 变量或表达式 typeof(变量或表达式) var n="asda"; console ...

  2. Java:反射小记

    Java:反射小记 对 Java 中的 反射,做一个微不足道的小小小小记 概念 Java 反射指的是在 Java 程序运行状态中,对于任何一个类,都可以获得这个类的所有属性和方法:对于给定的一个对象, ...

  3. Alpha-功能规格说明书

    项目 内容 这个作业属于哪个课程 2021春季软件工程(罗杰 任健) 这个作业的要求在哪里 团队项目-计划-功能规格说明书 一.引言 1. 项目简介 项目团队:删库跑路对不队 项目名称:题士 项目内容 ...

  4. camera isp(Image Signal Processor)

    1. 目标[52RD.com] 手机摄像头模组用ISP功能模块的市场走向及研发方向.为能够正确认识手机摄像模组行业提供技术及市场依据.[52RD.com] 2. ISP在模组上的应用原理[52RD.c ...

  5. 【做题记录】[NOIP2011 提高组] 观光公交

    P1315 [NOIP2011 提高组] 观光公交 我们想在 \(k\) 次加速每一次都取当前最优的方案加速. 考虑怎样计算对于每一条边如果在当前情况下使用加速器能够使答案减少的大小. 如果当前到达某 ...

  6. hdu 2955 Robberies(背包DP)

    题意: 小偷去抢银行,他母亲很担心. 他母亲希望他被抓的概率真不超过P.小偷打算去抢N个银行,每个银行有两个值Mi.Pi,Mi:抢第i个银行所获得的财产 Pi:抢第i个银行被抓的概率 求最多能抢得多少 ...

  7. popStar手机游戏机机对战程序

    DFS算,五分钟如果答案没有更新,那个解一般来说就很优了. #include <cstdio> #include <iostream> #include <string. ...

  8. 近期业务大量突增微服务性能优化总结-3.针对 x86 云环境改进异步日志等待策略

    最近,业务增长的很迅猛,对于我们后台这块也是一个不小的挑战,这次遇到的核心业务接口的性能瓶颈,并不是单独的一个问题导致的,而是几个问题揉在一起:我们解决一个之后,发上线,之后发现还有另一个的性能瓶颈问 ...

  9. Redis 专栏(使用介绍、源码分析、常见问题...)

    一.介绍相关 说Redis : 介绍Redis特性,使用场景,使用Jedis操作Redis等. 二.源码分析 1. 数据结构 Redis源码分析(sds):Redis自己封装的C语言字符串类型. Re ...

  10. Qt Creator 常用快捷键 详细总结

    下面是我总结的一些Qt Creator 常用快捷键 ,可以大大提高我们使用Qt开发项目的效率!! Qt Creator 常用快捷键 快捷键 介绍 F1 查看帮助文档 Shift + F2 函数的声明和 ...