Python uses the pillow library to add watermark widgets to pictures in batches

Hi everyone, hello! I am a cheese who loves to touch fish ❤

This time use python code to
add watermarks to photos in batches❤

Please add a picture description

Add watermark logic

"""
    为当指定目录下所有的图片文件添加水印
"""

import os

from PIL import Image
import PIL.PngImagePlugin

logo_filename = 'catlogo.png'
logo_img = Image.open(logo_filename) 

target_dir = 'assets'

work_dir = target_dir + '_watermark'
if not os.path.exists(work_dir):
    os.makedirs(work_dir)

for filename in os.listdir(target_dir):

    if not (filename.endswith('.jpg') or filename.endswith('.png')) or filename == logo_filename:
        continue

    filename_load_path = os.path.join(target_dir, filename)

    cat_img = Image.open(filename_load_path)  
    cat_img_width, cat_img_height = cat_img.size

    logo_img_copy = logo_img.copy()
    min_side = cat_img_width if cat_img_width < cat_img_height else cat_img_height
    logo_img_copy = logo_img_copy.resize((int(min_side / 6), int(min_side / 6)))

    logo_width, logo_height = logo_img_copy.size
    cat_img.paste(logo_img_copy, (0, cat_img_height - logo_height), logo_img_copy)

    cat_img.save(os.path.join(work_dir, filename))

Please add a picture description

encapsulated as a function

Later, we need to combine tkinter to build a tool,
so we need to encapsulate the original code

import os

from PIL import Image
import PIL.PngImagePlugin


def add_watermark(logo_filename, target_dir):

    logo_img = Image.open(logo_filename) 

    work_dir = target_dir + '_watermark'
    if not os.path.exists(work_dir):
        os.makedirs(work_dir)


    for filename in os.listdir(target_dir):

        if not (filename.endswith('.jpg') or filename.endswith('.png')) or filename == logo_filename:
            continue
        filename_load_path = os.path.join(target_dir, filename)
        cat_img = Image.open(filename_load_path)  # type:PIL.PngImagePlugin.PngImageFile
        cat_img_width, cat_img_height = cat_img.size
        logo_img_copy = logo_img.copy()
        min_side = cat_img_width if cat_img_width < cat_img_height else cat_img_height
        logo_img_copy = logo_img_copy.resize((int(min_side / 6), int(min_side / 6)))

        logo_width, logo_height = logo_img_copy.size
        cat_img.paste(logo_img_copy, (0, cat_img_height - logo_height), logo_img_copy)

        cat_img.save(os.path.join(work_dir, filename))

Please add a picture description

Use tkinter to write widgets

import tkinter as tk
from tkinter import filedialog
from utils import add_watermark

root = tk.Tk()
root.title('水印工具')
root.geometry('500x300')

dir_var = tk.StringVar()
file_var = tk.StringVar()
status_var = tk.StringVar()

input_dir_frame = tk.Frame()
input_dir_frame.pack()
tk.Label(input_dir_frame, text='文件夹:').pack(side=tk.LEFT)
tk.Entry(input_dir_frame, width=40, textvariable=dir_var).pack(side=tk.LEFT)
input_btn = tk.Button(input_dir_frame, text='选择')
input_btn.pack()

input_file_frame = tk.Frame()
input_file_frame.pack()
tk.Label(input_file_frame, text='水印文件:').pack(side=tk.LEFT)
tk.Entry(input_file_frame, width=40, textvariable=file_var).pack(side=tk.LEFT)

input_file_btn = tk.Button(input_file_frame, text='选择')
input_file_btn.pack()

add_watermark_btn = tk.Button(root, text='添加水印')
add_watermark_btn.pack()
tk.Label(root, textvariable=status_var).pack()


def load_dir_path():
    dir_path = filedialog.askdirectory()
    dir_var.set(dir_path)


def load_file_path():
    file_path = filedialog.askopenfilename()
    file_var.set(file_path)


input_btn.config(command=load_dir_path)
input_file_btn.config(command=load_file_path)


def add_watermark2():
    add_watermark(file_var.get(), dir_var.get())
    status_var.set('水印添加完毕')


add_watermark_btn.config(command=add_watermark2)

if __name__ == '__main__':
    root.mainloop()

Please add a picture description

Finally package the program

python -m venv venv 
venv\Scripts\activate.bat
pip install pillow 
pip install pyinstaller 
pyinstaller -F -w watermark.py

Guess you like

Origin blog.csdn.net/m0_74872863/article/details/130311572