Home > Article > Backend Development > Python batch watermarking only requires one line of commands!
When working, especially self-media workers, watermark adding tools are necessary to protect intellectual property images. There are many online/downloaded watermark adding tools on the Internet, but they may There are at least one of the following problems:
1. Online tools need to upload pictures to the other party's server, and the information is not secure.
2. Many tools do not have batch processing capabilities.
3. Many tools have too few customizable functions, such as watermark transparency, fonts, etc.
4. The operation is cumbersome.
Now as long as you know how to use commands, we can teach you how to use Python to add watermarks to pictures super easily, and it has the following features:
1. Supports custom watermark fonts.
2. Support custom text content and color.
3. Support batch processing.
4. Support setting the space between watermarks.
5. Support setting watermark font size.
6. Support setting transparency.
7. Your own code, safe.
We need to use the 2Dou open source project:
https://www.php.cn/link/4b5b81483048c8942ed00caaa17b9535
Very useful open source project, thanks to the original author.
There are three ways to download this project:
1. If your network can access github, you can enter the page, click clone or download and then click Download Zip.
2. If you have downloaded git, you can use cmd/terminal to enter the folder where you want to place it, and enter the command:
git clone https://www.php.cn/link/4b5b81483048c8942ed00caaa17b9535.git
3. If you don’t have one, you can directly reply to the watermark in the background of the Python Practical Collection official account Download the complete code for the repaired version of this article.
Download and unzip it to any folder you want to place it. It is best not to include Chinese names in the path. If you downloaded using the first two methods and are a Windows system user, be careful to change the font file name of the project to English. In addition, there is also a place in marker.py that needs to be changed. As follows:
Change the Blue Bird Huaguang Jane Amber.ttf in the font folder to bird.ttf. It doesn’t matter what the name is. The key point is not to use a Chinese name, otherwise pillow The modified file will not be available.
Note that the tenth line in the marker.py file should be changed to the corresponding name, corresponding to the font file name in the font folder.
We just mentioned the pillow library. The operation of this package requires the use of this third-party library, which is specially used to process images. Open CMD/Terminal and enter the following command to install:
pip install pillow
After the installation is complete, we can try it! The most common example is as follows. Place the pictures you need to watermark in the input folder of the project, then enter the folder where you store the project in cmd/Terminal and enter the following commands:
python marker.py -f ./input/baby.jpg -m python实用宝典
each The meaning of the parameters is as follows:
-f File path: It is the path of your picture
-m Text content: It is the content of the watermark you want to make
The parameters are not The setting is the default value. After the operation is completed, the corresponding watermarked picture will appear in the output folder. The effect is as follows:
Add watermark
The default watermark color is...shit yellow picture?
But it doesn’t matter, we can modify its color and add the -c parameter! (The default format of the parameter is # followed by 6 hexadecimal digits). Using the image tool, we can find the value of your favorite color:
Then we enter the command:
python marker.py -f ./input/baby.jpg -m python实用宝典 -c #232862
Success ! Take a look at the effect:
#Modify the watermark color
Yeah! It looks better, but it seems that the color of the watermark is a bit dark. We can modify the transparency to make it lighter. The default transparency is 0.15, which can make this value smaller. Set the opacity parameter:
python marker.py -f ./input/baby.jpg -m python实用宝典 -c #232862 --opacity 0.08
The result is as follows:
The watermark becomes more transparent
In fact, there are other parameters, we will not show them one by one, there are these parameters in total:
接下来给大家试试批量处理功能,首先把所有图片放置到项目的input文件夹下:
然后输入命令里,指定文件夹即可!
python marker.py -f ./input -m python实用宝典 -c #232862 --o
你会看到input文件夹名后没有/baby.jpg了,这表明将input文件夹下所有的图片打水印。
看到 文件名 succes 则说明批处理成功!
还有一个隐藏功能!如果你想要修改字体也可以哦!还记得我们前面怎么修复windows的中文名问题吗?如图,你只要将新的字体文件放到font文件夹下,然后修改TTF_FONT变量里的字体名字,与font文件夹下的新字体名字相对应即可改成你想要的字体了图片!
修改字体
我们的文章到此就结束啦,如果你希望我们今天的Python教程,请持续关注我们,如果对你有帮助,麻烦在下面点一个赞/在看哦图片有任何问题都可以在下方留言区留言,我们都会耐心解答的!
The above is the detailed content of Python batch watermarking only requires one line of commands!. For more information, please follow other related articles on the PHP Chinese website!