程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python tips: two lines of code to batch add watermarks to images, which is too simple~

編輯:Python

List of articles

  • Let me beep first
  • preparation
  • Code parsing
    • 1、 Add a watermark to a single picture
    • 2、 Batch add multiple image watermarks

Let me beep first

A super simple case , The one with hands ~


We usually add watermark to the picture , But not PS, I have to ask my friends for help , however , This time you find friends , Next time your sister , Xuemei , Female student , Female colleagues need your help , Are you still looking for friends ?

What a shame , You can't learn by yourself ~

And if there are many pictures ,PS It's still quite slow , use Python One click generation , Efficiency bars .

preparation

The environment I use here is Python and pycharm

If you add a watermark to an image , Just two lines of code .

The module used this time is filestools , Third-party module , We need to install it manually .

win+r Open the search box , Input cmd Press OK to pop up the command prompt window , Input pip install filestools You can install it successfully .


The installation and download speed is too slow , Or wrong , You can see the top article on my homepage , The content in this aspect was explained in detail .

Code parsing

1、 Add a watermark to a single picture

Module import

How do you use it? filestools Module? ?

First, import a method in the module ,watermarker , light marker , then import add_mark

from watermarker.marker import add_mark

add_mark Many methods have been included

file Yes add watermark photo ,mark What words are used as watermarks ,out Place of preservation ,color Color ,size Watermark font size ,opacity The opacity ,space Font spacing ,angle Rotation Angle .

Try any picture

Now I want to add : Life is too short , Learn quickly python

Let's add pictures and words

from watermarker.marker import add_mark
add_mark('img.png, mark=' Life is too short , Learn quickly python')

You can see the effect

2、 Batch add multiple image watermarks

Just look for a picture without watermark , I'll show you my little sister's picture .

If you add pictures in batches , Need to use OS modular .

Import it

from watermarker.marker import add_mark
import os

use os The module reads all the contents of the folder , Use files receive .

files = os.listdir('img_1\\')

A print

from watermarker.marker import add_mark
import os
files = os.listdir('img_1\\')
print(files)

effect


Now all the file names are

adopt for Loop traversal is printed out

for file in files:

Then add the file , What we read is img_1\ What's in it , Then transfer all the contents of this file , When saving data , Will be kept here .

from watermarker.marker import add_mark
import os
files = os.listdir('img_1\\')
print(files)
for file in files:
add_mark(f'img_1\\{
file}', mark=' Life is too short , Learn quickly python')

I won't open them one by one

Change the transparency again

It is the default 0.15, I'll change it here to 0.5 try

from watermarker.marker import add_mark
import os
files = os.listdir('img_1\\')
print(files)
for file in files:
add_mark(f'img_1\\{
file}', mark=' Life is too short , Learn quickly python', opacity=0.5)


So it's much clearer ~

Need source code and Python Click the business card below for information

That's all for sharing today , Follow me to learn more interesting and useful Python Tips !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved