Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to add image watermarks in batches by Python

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge about "Python how to add image watermarks in batches". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

I. Foreword

Pirates are very common these days, and many people use images without noting where they came from or mentioning the author. Watermarks are a good thing at this time. We can watermark the image and share it, so that others can know about the author of the image.

II. Text watermark

When adding watermarks, we often use text watermarks. For example,@ZackSock,@juejin:ZackSock, etc. This watermark is very simple to add, just need to use the Pillow module to add text operation, the code is as follows:

from PIL import Image, ImageFont, ImageDraw#Load font = ImageFont.truetype ('msyh. ttc', 60)#Load image im = Image.open ('im.jpg ')#Create brush drawer = ImageDraw.Draw(im)#Prepare text text = '@ZackSock'#Draw text drawer.text((0, 0), text, (0, 255, 0), font)im.show()

There are a few things to note here, the first is the watermark size. The watermark size is also the text size, which is composed of:

font = ImageFont.truetype('msyh.ttc', 60)

The second parameter is determined by, so we can adjust the watermark size by adjusting this parameter.

The second point is the watermark position and watermark color, this is through:

drawer.text((0, 0), text, (0, 255, 0), font)

The first and third parameters are determined, where (0, 0) represents the coordinates of the upper left corner of the watermark, and (0, 255, 0) represents the RGB value of the watermark. For specific adjustment, please refer to the RGB color table.

III. Picture watermark

Text watermark is more convenient to use, but sometimes we are more accustomed to a representative logo as a watermark, this time you can image watermark, the code is as follows:

from PIL import Imageim = Image.open ('origin.png ').convert ('RGBA')watermark = Image.open ('watermark.png ').convert ('RGBA')w, h = im.sizewatermark.thumbnail((w//6, h//6))r, g, b, a = watermark.split()#Get watermark size w2, h3 = watermark.size#Calculate position x = w-w2y = h-h3#Paste im.paste(watermark, (x, y), mask=a)im.show()

The renderings are as follows:

"Python how to batch add image watermark" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report