Sometimes I want to add my own watermarks to images to prevent others from stealing them, so today I’m going to share with you how to use Python to watermark our images. Let’s look at the effect first.

You can see the lower right corner of our public number name watermark, is not super powerful? So let’s look at the code. It’s less than 20 lines.

The premise needs to download good library PIL, no first to download, here is not to say.

Isn’t it very simple, just change it and you can add it in batches, and you can change the font and change the text position to achieve different effects.

You only need to change the next two lines of code

Font = imagefont. truetype('C:\Windows\Fonts\ hys5gfm.ttf ', Text_xy = (layer.size[0]//2 -text_size_x //2, layer.size[1] -text_size_y)Copy the code

It should be noted that the Chinese font cannot be displayed when adding watermarks because the font you set does not have Chinese, so you need to replace it with a corresponding Chinese font.

We can also add images or logos to our images, like this:

It would also be nice to put our logo on the picture, which makes it even more impossible to pirate, and less than 10 lines of code. Besides: Life is too short, I use Python:)

Python is probably capable of doing everything except giving birth, what are you waiting for, knock on it!!

Ps: original is not easy, if you think the article is good, welcome to click and forward support

Daily learning python

Code is not just buggy, it’s beautiful and fun