In order to protect the CSDN copyright, I can add a watermark to the picture with one line of code!

Copyright is very important. For a picture, it may be your carefully crafted mind map, or your carefully designed logo. You may spend a lot of time to get it, but in the end it is directly carried by others and used, so angry!

Based on this, in this article, I will take everyone to learn how to give you 图片加水印, just one line of code , anyone can learn.

Introduction to the filestools library

The Python library introduced to you today is called filestools. You can use the following commands directly and use them after installation.

pip install filestools --index-url=http://mirrors.aliyun.com/pypi/simple -U

This library integrates a total of 4 functions. All 4 libraries have been migrated to the filestools library. Therefore, after installing this library, you can use all 4 functions. You only need to use the corresponding functions and import the corresponding modules respectively. That's it.

# 给图片加水印
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 the jupyter notebook to view it.
insert image description here
Tell everyone about the meaning of the 8 parameters in the add_mark() method.

  • file: the photo to be watermarked;
  • mark: which words to use as watermarks;
  • out: the location to save after adding the watermark;
  • color: the color of the watermark font, the default color #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 space between the 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.

One line of code to watermark an image

The original image looks like this:
insert image description here
use the following line of code:

from watermarker.marker import add_mark
add_mark(file=r"aixin.jpg", out=r"C:\Users\Administrator\Desktop\python三剑客\加盟店爬虫", mark="快学Python", opacity=0.2, angle=45, space=30)

After adding the watermark, it looks like this:
insert image description here
Finally, I will explain to you the meaning of this line of code.

add_mark(file=r"aixin.jpg", out=r"C:\Users\Administrator\Desktop\python三剑客\加盟店爬虫", mark="快学Python", opacity=0.2, angle=45, space=30)

Meaning:aixin.jpg Add a watermark to the picture named in the current working environment . The words of 快学Pythonthe watermark are, the transparency of the 0.2watermark is, the rotation angle of the 45°watermark is, and the interval between the watermarks is 30a space. After adding the watermark, finally save the picture 一个指定目录under .

If you want to learn the filestools library in detail, you can refer to the following website:
https://pypi.org/project/filestools/

Guess you like

Origin blog.csdn.net/weixin_41261833/article/details/119375419