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

Python script for batch watermarking of images

編輯:Python

Let me write it out front :

First of all, I would like to apologize to all the fans .EICCIE While upgrading the outline , It also innovated the original outline 40% The content of , It means that I am also a technology lover , Need to learn 40% New skills .
I've been busy learning new technologies during this period , Acquire new skills .
Campus network SDN、 Wide area network SDN、 cloud 、 Data Center 、 Hongmeng 、java、 big data 、 data storage 、python、 Network programmability and automation , These lovely things make me forget to eat and sleep , And forget you , I'm sorry .
Take the initiative to share a practical script today , In good faith .

Batch add watermark to the picture python Script

This script is written by Uncle Da , It is mainly used for daily work and sharing .
There is not much time , There is no optimization , Make do with , The good thing is that I am not in a hurry .
Borrowed an open source third-party library , Realize batch reading of pictures , Then add a watermark , Then save the effect .
If you want to use a watermarked script , You need to be on your own python Resolve the third-party libraries used in the installation scripts in the environment .

Import the installed third-party library

import os
from PIL import Image, ImageDraw, ImageFont

  • 1.
  • 2.

Define a function to add watermark

# Define functions to add watermarks ,font Is a font file ,image For adoption Image.open() Method to open a picture object ,text Is the watermark string 
def AddWaterMark(font, image, text):
# Open the font file , Prepare a font file by yourself 
Font = ImageFont.truetype(font, 24)
# Add background 
new_img = Image.new('RGBA', (image.size[0] * 3, image.size[1] * 3), (0, 0, 0, 0))
new_img.paste(image, image.size)
# Add watermark 
font_len = len(text)
# Convert image to RGBA Images 
rgba_image = new_img.convert('RGBA')
# Generate an image as large as the image to be watermarked 
text_overlay = Image.new('RGBA', rgba_image.size, (255, 255, 255, 0))
# drawing 
image_draw = ImageDraw.Draw(text_overlay)
# Draw multiple watermarks , Set the location of the watermark 
for i in range(0, rgba_image.size[0], font_len * 20):
for j in range(0, rgba_image.size[1], 50):
# Set text color and transparency and position 
image_draw.text((i, j), text, font=Font, fill=(255, 102, 102, 80))
# Watermark direction setting 
text_overlay = text_overlay.rotate(20)
# Overlay the generated image on the image to be watermarked 
image_with_text = Image.alpha_composite(rgba_image, text_overlay)
# Cutting pictures 
image_with_text = image_with_text.crop((image.size[0], image.size[1], image.size[0] * 2, image.size[1] * 2))
return image_with_text

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

Define a function to get the file object

# Defined function , Get all files in the current directory and subdirectories 
def GetAllPngs(PngPath, All_IMG_NameList):
CurrentList = os.listdir(PngPath) # Read all objects in the current directory to form a list 
for obj in CurrentList: # Traverse the list 
Current_path = os.path.join(PngPath,obj) # Form all objects into absolute paths 
if os.path.isdir(Current_path): # If it is a directory, perform self iteration 
GetAllPngs(Current_path, All_IMG_NameList) # Check for subdirectories 
elif os.path.isfile(Current_path):
All_IMG_NameList.append(Current_path) # Return absolute path and file name 
else:
pass
return All_IMG_NameList

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

Define the arguments and call the function to complete the work

if __name__ == '__main__':
font = 'd:/mingliu.ttc' # Font file directory 
text = ' Produced by Uncle Qianyi Tangda www.qytang.com' # Watermark text to add ()
PngPath = 'D:/temp/' # Generated Png Picture catalog 
AllPngNameList = [] # Used to store the absolute path of the picture before processing 
GetAllPngs(PngPath, AllPngNameList) # Get the absolute path list of the image before processing 
# Use for Loop through the list , Watermark each picture and save it 
for IMG in AllPngNameList:
image = Image.open(IMG)
MarkedPng = AddWaterMark(font , image, text)
Result = IMG.replace('jpg', 'png')
MarkedPng.save(Result)

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

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