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:
- -f 参数,指定打水印的文件,如果你想打印整个文件夹,则输入该文件夹路径即可。
- -m 参数,指定水印内容。
- -o 参数,指定输出水印文件的位置,默认为output文件夹。
- -c 参数,指定水印的颜色,默认值为shi..啊不,黄色,#8B8B1B.
- -s 参数,指定水印与水印之间的空隙,默认值为75.
- -a 参数,指定水印的旋转角度,我们的例子中都是默认值30度。
- --size参数,指定水印文本字体大小,默认值为50。
- --opacity参数,指定透明度,默认为0.15,数值越小越透明。
接下来给大家试试批量处理功能,首先把所有图片放置到项目的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!

TomergelistsinPython,youcanusethe operator,extendmethod,listcomprehension,oritertools.chain,eachwithspecificadvantages:1)The operatorissimplebutlessefficientforlargelists;2)extendismemory-efficientbutmodifiestheoriginallist;3)listcomprehensionoffersf

In Python 3, two lists can be connected through a variety of methods: 1) Use operator, which is suitable for small lists, but is inefficient for large lists; 2) Use extend method, which is suitable for large lists, with high memory efficiency, but will modify the original list; 3) Use * operator, which is suitable for merging multiple lists, without modifying the original list; 4) Use itertools.chain, which is suitable for large data sets, with high memory efficiency.

Using the join() method is the most efficient way to connect strings from lists in Python. 1) Use the join() method to be efficient and easy to read. 2) The cycle uses operators inefficiently for large lists. 3) The combination of list comprehension and join() is suitable for scenarios that require conversion. 4) The reduce() method is suitable for other types of reductions, but is inefficient for string concatenation. The complete sentence ends.

PythonexecutionistheprocessoftransformingPythoncodeintoexecutableinstructions.1)Theinterpreterreadsthecode,convertingitintobytecode,whichthePythonVirtualMachine(PVM)executes.2)TheGlobalInterpreterLock(GIL)managesthreadexecution,potentiallylimitingmul

Key features of Python include: 1. The syntax is concise and easy to understand, suitable for beginners; 2. Dynamic type system, improving development speed; 3. Rich standard library, supporting multiple tasks; 4. Strong community and ecosystem, providing extensive support; 5. Interpretation, suitable for scripting and rapid prototyping; 6. Multi-paradigm support, suitable for various programming styles.

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
