PIL如何批量给图片添加文字水印?

简介: PIL如何批量给图片添加文字水印?

1 简单引入

  • 平常我们想给某些图片添加文字水印,方法有很多,也有很多的工具可以方便的进行;
  • 今天主要是对PIL库的应用,结合Python语言批量对图片添加水印;
  • 这里需要注意的是图片的格式,不能为JPG或JPEG,因为这两种格式的图片不支持透明度设置。

2 关于PIL

之前有的文章已经详细介绍过,这里不再赘述了。

  • PILPython的一个图像处理库,支持多种文件格式;
  • PIL提供强大的图像处理和图形处理能力;
  • 可完成对图像的缩放、裁剪、叠加以及图像添加线条、图像和文字等操作。
  • 安装的话,使用以下命令:
pip install Pillow

在这里插入图片描述

3 本文涉及的PIL的几个类

模块或类 说明
image模块 用于对图像就行处理
ImageDraw 2D图像对象
ImageFont 存储字体
ImageEnhance 图像增强

4 实现原理

本文主要目的是批量对某个文件夹下的图片进行添加水印,原理如下:

  • 对水印内容设置;
  • 使用Image对象的open()方法打开原始图片;
  • 使用Image对象的new()方法新建存储水印图片对象;
  • 使用Image.Draw.Draw对象的text()绘制水印文字;
  • 使用ImageEnhance中的Brightness中的enhance()设置水印透明度。

5 实现过程

5.1 原始图片

  • 我们设置一个原始图片的存储目录,比如:
F:\python_study\image\image01
  • 这个文件夹中存放了多个png格式的图片:
    在这里插入图片描述
  • 这里的一个原始图片为:
    在这里插入图片描述

    5.2 导入相关模块

  • 导入需要的几个PIL中的模块或类:
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
import os

5.3 初始化数据

  • 我们这里预想的是,通过用户自己手动输入相关信息,比如图片的存储路径,需要设置的水印文字,通过输入的方式设置水印的位置,设置水印的透明度等;
class TestText():
    def __init__(self):
        super(TestText, self).__init__()
        self.pic_path = input('图片路径:')
        self.pic_text = input('水印文字:')
        self.p_flag = int(input('水印位置(1:左上角,2:左下角,3:右上角,4:右下角,5:居中):'))
        self.a = float(input('水印透明度(0—1之间的1位小数):'))

5.4 水印字体设置

  • 这里我们先看下ImageFont.truetype源码:
def truetype(font=None, size=10, index=0, encoding="", layout_engine=None):
    """
    Load a TrueType or OpenType font from a file or file-like object,
    and create a font object.
    This function loads a font object from the given file or file-like
    object, and creates a font object for a font of the given size.

    Pillow uses FreeType to open font files. On Windows, be aware that FreeType
    will keep the file open as long as the FreeTypeFont object exists. Windows
    limits the number of files that can be open in C at once to 512, so if many
    fonts are opened simultaneously and that limit is approached, an
    ``OSError`` may be thrown, reporting that FreeType "cannot open resource".
    A workaround would be to copy the file(s) into memory, and open that instead.

    This function requires the _imagingft service.

    :param font: A filename or file-like object containing a TrueType font.
                 If the file is not found in this filename, the loader may also
                 search in other directories, such as the :file:`fonts/`
                 directory on Windows or :file:`/Library/Fonts/`,
                 :file:`/System/Library/Fonts/` and :file:`~/Library/Fonts/` on
                 macOS.

    :param size: The requested size, in pixels.
    :param index: Which font face to load (default is first available face).
    :param encoding: Which font encoding to use (default is Unicode). Possible
                     encodings include (see the FreeType documentation for more
                     information):
  • 它的大意就是从系统中的字体库中读取字体;
  • 那我们的系统字体在哪里呢?
  • 系统字体在
C:\Windows\Fonts
  • 如下:
    在这里插入图片描述
  • 我们随便选一个自己喜欢的字体,复制下名字即可:
self.font = ImageFont.truetype("cambriab.ttf", size=35)

5.5 打开原始图片并新建存储对象

  • 打开原始图片,并转换为RGB
image = Image.open(img).convert('RGBA')
  • 创建绘制对象:
new_img = Image.new('RGBA', image.size, (255, 255, 255, 0))
image_draw = ImageDraw.Draw(new_img)

5.6 计算图片和水印的大小

  • 图片大小:
w, h = image.size
  • 文字大小:
w1 = self.font.getsize(self.pic_text)[0]  # 获取字体宽度
h1 = self.font.getsize(self.pic_text)[1]  # 获取字体高度

5.7 选择性设置水印文字

  • 通过if语句来实现:
 if self.p_flag == 1:  # 左上角
      location = (0, 0)
 elif self.p_flag == 2:  # 左下角
      location = (0, h - h1)
 elif self.p_flag == 3:  # 右上角
      location = (w - w1, 0)
 elif self.p_flag == 4:  # 右下角
      location = (w - w1, h - h1)
 elif self.p_flag == 5:  # 居中
      location = (h/2, h/2)

5.8 绘制文字并设置透明度

  • 绘制文字:
image_draw.text(location, self.pic_text, font=self.font, fill="blue")
  • 设置透明度
 transparent = new_img.split()[3]
 transparent = ImageEnhance.Brightness(transparent).enhance(self.a)
 new_img.putalpha(transparent)

 Image.alpha_composite(image, new_img).save(img)

5.9 遍历获取图片文件并调用绘制方法

 text_pic = TestText()
    try:
        file_list = os.listdir(text_pic.pic_path)
        for i in range(0, len(file_list)):
            filepath = os.path.join(text_pic.pic_path, file_list[i])  
            if os.path.isfile(filepath): 
                filetype = os.path.splitext(filepath)[1] 
                if filetype == '.png': 
                    text_pic.test_text(filepath) 
                else:
                    print("图片格式有误,无法添加水印,请使用png格式图片")
        print('批量添加水印完成')
    except:
        print('输入的文件路径有误,请检查~~')

6 完整源码

# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/21 
# 文件名称:test_text_pic.py
# 作用:批量给图片添加文字水印和图片水印
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson


from PIL import Image, ImageDraw, ImageFont, ImageEnhance
import os


class TestText():
    def __init__(self):
        super(TestText, self).__init__()
        self.pic_path = input('图片路径:')
        self.pic_text = input('水印文字:')
        self.p_flag = int(input('水印位置(1:左上角,2:左下角,3:右上角,4:右下角,5:居中):'))
        self.a = float(input('水印透明度(0—1之间的1位小数):'))

        # 设置字体
        self.font = ImageFont.truetype("cambriab.ttf", size=35)

    # 文字水印
    def test_text(self, img):
        global location
        image = Image.open(img).convert('RGBA') 
        new_img = Image.new('RGBA', image.size, (255, 255, 255, 0)) 
        image_draw = ImageDraw.Draw(new_img) 
        w, h = image.size  # 图片大小
        w1 = self.font.getsize(self.pic_text)[0]  # 字体宽度
        h1 = self.font.getsize(self.pic_text)[1]  # 字体高度

        # 设置水印文字位置
        if self.p_flag == 1:  # 左上角
            location = (0, 0)
        elif self.p_flag == 2:  # 左下角
            location = (0, h - h1)
        elif self.p_flag == 3:  # 右上角
            location = (w - w1, 0)
        elif self.p_flag == 4:  # 右下角
            location = (w - w1, h - h1)
        elif self.p_flag == 5:  # 居中
            location = (h/2, h/2)
        # 绘制文字
        image_draw.text(location, self.pic_text, font=self.font, fill="blue")

        # 设置透明度
        transparent = new_img.split()[3]
        transparent = ImageEnhance.Brightness(transparent).enhance(self.a)
        new_img.putalpha(transparent)

        Image.alpha_composite(image, new_img).save(img)


if __name__ == "__main__":
    text_pic = TestText()
    try:
        file_list = os.listdir(text_pic.pic_path) 
        for i in range(0, len(file_list)): 
            filepath = os.path.join(text_pic.pic_path, file_list[i])
            if os.path.isfile(filepath): 
                filetype = os.path.splitext(filepath)[1] 
                if filetype == '.png': 
                    text_pic.test_text(filepath) 
        print('批量添加水印完成')
    except:
        print('输入的文件路径有误,请检查~~')

7 显示效果

  • 运行过程:
D:\Python37\python.exe F:/python_study/python_project/test_text_pic.py
图片路径:F:\python_study\image\image01
水印文字:@虫无涯
水印位置(1:左上角,2:左下角,3:右上角,4:右下角,5:居中):1
水印透明度(01之间的1位小数):0.5
F:/python_study/python_project/test_text_pic.py:32: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
  w1 = self.font.getsize(self.pic_text)[0]  # 获取字体宽度
F:/python_study/python_project/test_text_pic.py:33: DeprecationWarning: getsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use getbbox or getlength instead.
  h1 = self.font.getsize(self.pic_text)[1]  # 获取字体高度
批量添加水印完成
  • 不同位置的水印效果:
    在这里插入图片描述
  • 居中效果:
    在这里插入图片描述
目录
相关文章
|
2月前
|
文字识别 数据挖掘 网络安全
Python实现avif图片转jpg格式并识别图片中的文字
在做数据分析的时候有些数据是从图片上去获取的,这就需要去识别图片上的文字。Python有很多库可以很方便的实现OCR识别图片中的文字。这里介绍用EasyOCR库进行图片文字识别。easyocr是一个比较流行的库,支持超过80种语言,识别率高,速度也比较快。
30 2
|
人工智能 文字识别 API
20行代码教你如何批量提取图片中文字
大家好,我是志斌~ 之前志斌在考研的时候遇到了一个问题,就是要将图片中的文字给提取出来,当时是J哥帮忙搞出来的,现在已经考完研了,也学会了提取方式,现在来给大家分享一下。
693 0
20行代码教你如何批量提取图片中文字
|
数据安全/隐私保护
生活中常用的图片去水印方法有哪些呢
有时候我们想换头像/微信背景墙了 是不是第一时间想到的是去某书逛逛,有时候看到有些博主分享的壁纸或者表情包等,忍不住的想保存下来,很多人应该还不知道如何下载吧,今天分享我的三个操作方法
【图片操作】给图片添加滤镜
现在我们都喜欢给图片添加滤镜,现在很多相机也自带了许多滤镜。我们可以在拍照的时候选择需要的滤镜。但是有时候我们需要给大量图片添加同样的滤镜,这个时候手动添加就非常麻烦了。为了方便,我们可以使用程序来帮我们完成添加滤镜的操作。
192 0
|
前端开发 JavaScript 搜索推荐
文字水印的几种实现方式
文字水印是一种强提示,一般会用在前端展示敏感、权利声明等场景下。我们简单介绍几种方法。
392 0
文字水印的几种实现方式
|
算法 数据安全/隐私保护 计算机视觉
用 OpenCV 去除图片中的水印,骚操作!
这两天在 Stackoverflow 上面看到了一个有趣的案例,是关于OpenCV 的一个讨论,讨论的主题就是如何用 OpenCV 来去除下面图片中的水印,原图如下;
用 OpenCV 去除图片中的水印,骚操作!
|
缓存 Java 数据安全/隐私保护
给图片加水印?这是我见过最简单的实现方式
大家好,我是指北君。 在项目中经常有需要在图片上添加水印的需求以及在某些场合下需要身份证图片,这时就可以对身份证上加水印防止被用于其他用途,java 在处理图片水印时不需要额外的第三方包,使用 BufferedImage 和 Graphics2D 就可以搞定
给图片加水印?这是我见过最简单的实现方式
|
Linux 数据安全/隐私保护 C语言
RGB源数据操作: 在图片上添加中文水印
RGB源数据操作: 在图片上添加中文水印
244 0
RGB源数据操作: 在图片上添加中文水印
|
C# 图形学 索引
上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片
原文:上传图片时,使用GDI+中重绘方式将CMYK图片转为RGB图片 我们知道,如果网站上传图片时,如果用户上传的是CMYK图片,那么在网站上将是无法显示的,通常的现象是出现一个红叉。
1167 0
|
前端开发 Android开发 数据安全/隐私保护
Android图片添加文字水印并保存水印文字图片到指定文件
Android图片添加文字水印并保存水印文字图片到指定文件 package zhangphil.test; import android.
2899 0