Super simple~ Python adds watermark, three lines of code teach you to add in batches

Environmental use:

  • Python 3.8
  • Pycharm

How to configure the python interpreter in pycharm?

  • Select file (file) >>> setting (setting) >>> Project (project) >>> python
    interpreter (python interpreter)
  • Click on the gear, select add
  • Add python installation path

How does pycharm install plugins?

  • Select file (file) >>> setting (setting) >>> Plugins (plugins)
  • Click Marketplace and enter the name of the plug-in you want to install. For example: for the translation plug-in, enter translation / for the Sinicization plug-in, enter Chinese
  • Select the corresponding plug-in and click install
  • After the installation is successful, the option to restart pycharm will pop up, click OK, and the restart will take effect

The module uses:

  • filestools (third-party module, needs to be installed)

Install python third-party modules:

  • win + R, enter cmd and click OK, enter the installation command pip install module name (pip install requests) and press Enter
  • Click Terminal (terminal) in pycharm to enter the installation command to install the download speed is too slow, or an error is reported, you can switch the mirror source installation

the code

1. Add watermark to a single picture

How to use the filestools module?

First import a method in the module, watermarker, click on marker, and then import add_mark

Original code. Click to receive [Remarks: Su]
from watermarker.marker import add_mark

The add_mark() method has a total of 8 parameters. If you don't know how to view the parameters, you can enter the following command in jupyter notebook to view them.

  • file: the photo to be watermarked;
  • mark: Which words are used as watermarks;
  • out: the saved location after adding the watermark;
  • color: the color of the watermark font, the default color is #8B8B1B;
  • size: The size of the watermark font, the default is 50;
  • opacity: the transparency of the watermark font, the default is 0.15;
  • space: the interval between watermark fonts, the default is 75 spaces;
  • angle: The rotation angle of the watermark font, the default is 30 degrees; Next, we only use one line of code to add a watermark to the image.
add_mark('C:\\Users\\青灯教育\\Desktop\\111\\1.jpg', mark='点赞、关注、收藏呀', opacity=0.5)

The link here is a link to a single picture~

You can click Image Properties —> Security —> Object Name to seePlease add a picture description

Running effect display

Ok, let's run it and see our effect

Original image:Please add a picture description
Renderings:

Please add a picture description
Please add a picture description
Please add a picture description

2. Add multiple image watermarks in batches

from watermarker.marker import add_mark
import os

# 用os模块读取文件夹所有内容 ,使用 files 接收。
files = os.listdir('C:\\Users\\青灯教育\\Desktop\\111')
for file in files:
    add_mark('C:\\Users\\青灯教育\\Desktop\\111\\'+file, mark='点赞、关注、收藏呀', opacity=0.5)

Please add a picture description

3. Details modification

modify color

Original code. Click to receive [Remarks: Su]
add_mark('C:\\Users\\青灯教育\\Desktop\\111\\68.jpg', mark='点赞、关注、收藏呀', opacity=0.5, color="#ffffff")

Please add a picture description

modify font size

add_mark('C:\\Users\\青灯教育\\Desktop\\111\\81.jpg', mark='点赞、关注、收藏呀', opacity=0.5,size=80)

Please add a picture description

modify angle

add_mark('C:\\Users\\青灯教育\\Desktop\\111\\101.jpg', mark='点赞、关注、收藏呀', opacity=0.5,angle=0)

Please add a picture description
For more details, you can study it yourself~

That's right, the following folder (that is, the file generated for you after running the code) should be deleted if you want to run it again, otherwise an error will be reported. Well, the article sharing is over
Please add a picture description
here

If you have any questions about the article, or other questions about python, you can leave a message in the comment area or private message me. If you think the
article I shared is good, you can follow me or give the article a thumbs up (/≧▽≦)/

Guess you like

Origin blog.csdn.net/sunanpython/article/details/128326483