Add a full image watermark to the image through Python's filestools library


foreword

Hello everyone, I am Kongkong star. In this article, I will share with you how to add full-image watermarks to pictures through Python's filestools library.


1. Introduction to filestools library

filestools currently includes four toolkits, which are tree directory display, file difference comparison, image watermarking and curl request to python code.

2. Install filestools

pip install filestools

3. Check the filestools version

pip show filestools

Name: filestools
Version: 0.1.3
Summary: Linux-like tree command, file difference comparison tool, image watermarking and curl request to python code.
Home-page: UNKNOWN
Author: Xiaoxiaoming
License: GPLv3
Requires: cchardet, Pillow, pyperclip, rich
Required-by:

4. Add full image watermark to the image

1. Import library

from watermarker.marker import add_mark

2. Add watermark

add_mark(file="demo.jpg",
         out="out",
         mark="空空star",
         color="white",
         size=30,
         opacity=0.3,
         angle=45,
         space=75)

3. Effect

5. Parameter adjustment comparison

1. Watermark color

Default #8B8B1B

1.1 Set color by name

# 通过名称设置颜色-黄色
color = 'yellow'

1.2 Set the color by RGB value

# 通过RGB值设置颜色-红色
color = (255, 0, 0)

1.3 Set the color by hexadecimal

# 通过十六进制设置颜色-绿色
color = '#6FE000'

2. The size of the watermark font

Default 30

# 左
size=30
# 右
size=50

3. Transparency of watermark

default 0.15

# 左
opacity=0.3
# 右
opacity=0.6

4. Watermark direct interval

Default 75 spaces

# 左
space=75
# 右
space=100

5. Watermark rotation angle

Default 30 degrees

# 左
angle=45
# 右
angle=-45


Summarize

Looking at marker.py, you can find that this watermark processing is based on the PIL library. You can also see the default value of the watermark font.

From the following code, you can also see the default values ​​of the relevant parameters.

def add_mark(file, mark, out="output", color="#8B8B1B", size=30, opacity=0.15, space=75, angle=30):
    if os.path.isdir(file):
        names = os.listdir(file)
        for name in names:
            image_file = os.path.join(file, name)
            add_mark2file(image_file, mark, out, color, size, opacity, space, angle)
    else:
        add_mark2file(file, mark, out, color, size, opacity, space, angle)

Guess you like

Origin blog.csdn.net/weixin_38093452/article/details/130341215