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 .
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 os
from PIL import Image, ImageDraw, ImageFont
# 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
# 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
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)