python给图片加水印详解

1、打开原图片,转换

from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
# 打开图片
img = Image.open('./小说阅读器.png').convert('RGBA')

convert()详解

返回此图像的转换后的副本。 对于“ P”模式,此方法可通过调色板转换像素。 如果省略模式,则选择一种模式,以便无需调色板即可表示图像和调色板中的所有信息。

当前版本支持“ L”,“ RGB”和“ CMYK”之间的所有可能的转换。 矩阵参数仅支持“ L”和“ RGB”。

在将彩色图像转换为灰度(模式“ L”)时,该库使用ITU-R 601-2亮度转换:

L = R * 299/1000 + G * 587/1000 + B * 114/1000

将灰度(“ L”)或“ RGB”图像转换为双色(模式“ 1”)图像的默认方法是使用Floyd-Steinberg抖动来近似原始图像的亮度水平。 如果dither为NONE,则所有大于128的值均设置为255(白色),所有其他值均设置为0(黑色)。 要使用其他阈值,请使用〜PIL.Image.Image.point方法。

当不使用矩阵参数从“ RGBA”转换为“ P”时,会将操作传递给〜PIL.Image.Image.quantize,并且抖动和调色板将被忽略。

参数:

mode–请求的模式。 参见:概念模式。
matrix–可选的转换矩阵。 如果给定,则应为包含浮点值的4元组或12元组。
dither –抖动方法,从模式“ RGB”转换为“ P”或从“ RGB”或“ L”转换为“ 1”时使用。 可用的方法为NONE或FLOYDSTEINBERG(默认)。 请注意,当提供** matrix **时,不使用该选项。
palette –从模式“ RGB”转换为“ P”时要使用的调色板。 可用的调色板是WEB或ADAPTIVE。
colors–用于“自适应”调色板的颜色数。 默认值为256。

返回值:
一个:py:class:〜PIL.Image.Image对象。

模式如下:

11位像素,黑白,每字节一个像素存储
L8位像素,黑白
P8位像素,使用调色板映射到任何其他模式
RGB3*8位像素,真彩色
RGBA4*8位像素,带透明度
CMYK4*8位像素,分色
YCbCr3*8位像素,彩色视频格式
I32位有符号整数像素
F32位浮点像素

2、新建空白图

# 新建一个空白图片,尺寸与打开的图片一致
image_null = Image.new('RGBA', img.size, (0,0,0,0))  

color的值在这个代码中是(0,0,0,0),前三个表示RGB三原色,0-255,最后一个参数表示透明度:数字越小越透明,越大越不透明,搭配前面的3个值,可以创建出不同颜色不同透明度的图片,效果如下

(0, 0, 0, 0):
在这里插入图片描述

(0, 0, 0, 110):
在这里插入图片描述
(0, 0, 0, 255):
在这里插入图片描述
(255, 0, 0, 110):
在这里插入图片描述

用给定的模式和尺寸创建一个新图像。

参数:

mode –用于新图像的模式。
size – 2元组,包含(宽度,高度)以像素为单位。
color –图像使用什么颜色。 默认为黑色。 如果给出的话,对于单频带模式,它应该是一个整数或浮点值;对于多频带模式,它应该是一个元组(每频带一个值)。 创建RGB图像时,还可以使用ImageColor模块支持的颜色字符串。 如果颜色为无,则不会初始化图像。

返回值:
一个:py:class:〜PIL.Image.Image对象。

3、设置水印字体

# 设置字体
font_t = ImageFont.truetype(r'C:\Windows\Fonts\STHUPO.TTF', 35)

从文件或类似文件的对象加载TrueType或OpenType字体,然后创建一个字体对象。 此函数从给定文件或类似文件的对象加载字体对象,并为给定大小的字体创建字体对象。

使用FreeType打开字体文件。 如果要在Windows上同时打开许多字体,请注意Windows将一次可以在C中打开的文件数限制为512。如果接近该限制,则可能会引发OSError ,报告FreeType“无法打开资源” 。
此功能需要_imagingft服务。

参数:
font –包含TrueType字体的文件名或类似文件的对象。 如果在此文件名中找不到该文件,则加载程序还可以在其他目录中搜索,例如Windows上的:file:fonts /目录或/ file:/ Library / Fonts /
size –请求的大小(以磅为单位)。
index –要加载的字体(默认为第一个可用的字体)。
encoding –使用哪种字体编码(默认为Unicode)。 可能的编码包括(有关更多信息,请参见FreeType文档):
layout_engine –要使用的布局引擎(如果可用):ImageFont.LAYOUT_BASICImageFont.LAYOUT_RAQM

返回值:
字体对象。

4、创建新的空白图

# 创建新的空白图片,将新建图片添入画板
new_null = ImageDraw.Draw(image_null)

用于PIL图像的简单2D绘图界面。

参数:
im –要绘制的图像。
mode –用于颜色值的可选模式。 对于RGB图像,此参数可以是RGB或RGBA(用于将图形混合到图像中)。 对于所有其他模式,此参数必须与图像模式相同。 如果省略,则模式默认为图像模式。

5、添加字体

# 在新建的图片上添加水印
new_null.text((image_null.size[0] - image_null.size[0]//2, image_null.size[1] - image_null.size[1]//1.5),
			  '水印字样', font = font_t, fill = (255, 0,0,105))

PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None)
Draws the string at the given position.

参数:
xy – Top left corner of the text.
text – Text to be drawn.
font – An ImageFont instance.
fill– Color to use for the text.

6、合并图片

# 合并两个图片
res_image = Image.alpha_composite(img, image_null)

PIL.Image.alpha_composite(im1, im2)
Alpha composite im2 over im1.

参数:
im1 – The first image.
im2 – The second image. Must have the same mode and size as the first image.

返回:
An Image object.

source:

def alpha_composite(im1, im2):
    """
    Alpha composite im2 over im1.

    :param im1: The first image.
    :param im2: The second image.  Must have the same mode and size as
       the first image.
    :returns: An :py:class:`~PIL.Image.Image` object.
    """

    im1.load()
    im2.load()
    return im1._new(core.alpha_composite(im1.im, im2.im))

7、保存图片

# 调用save方法将图片保存到本地
res_result.save('add_watermark.png')

Image.save(fp, format=None, **params)

Saves this image under the given filename. If no format is specified, the format to use is determined from the filename extension, if possible.

Keyword options can be used to provide additional instructions to the writer. If a writer doesn’t recognise an option, it is silently ignored. The available options are described later in this handbook.

You can use a file object instead of a filename. In this case, you must always specify the format. The file object must implement the seek, tell, and write methods, and be opened in binary mode.

参数:

file – File name or file object.
format – Optional format override. If omitted, the format to use is determined from the filename extension. If a file object was used instead of a filename, this parameter should always be used.
options – Extra parameters to the image writer.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值