当前位置: 首页 > news >正文

为图片添加水印(Python)

简介

刚好学了一下tkinter.colorchooser,然后……

优化了以前的代码,不过仍然是shi

功能

可自由添加水印内容、选择颜色、字体及字体大小、图片、水印的x、y位置

代码

# -*- coding: utf-8 -*-
# Environment    PyCharm
# File_name   visibleWaterMark |User    Pfolg
# 2024/8/7   19:30
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from tkinter import ttk, filedialog, messagebox, colorchooser
import tkinter as tk
import osdef watermark_Image(img_path, text, pos, color="#FFFFFF", textFont="STSONG.TTF", textSize=20):image = Image.open(img_path)drawing = ImageDraw.Draw(image)# 加载字体文件font = ImageFont.truetype(font=textFont, size=textSize)drawing.text(pos, text, fill=color, font=font)image.show()# img.save(output_path)# img = ".\\旅行.png"
# watermark_Image(img, ".\\output.png", 'Python', pos=(10, 10))
def visibleWaterMarkFindFile(x, text):win = tk.Tk()win.withdraw()file_path = filedialog.askopenfilename()x.set(file_path)try:image = Image.open(file_path)width, height = image.sizetext.set(f"图片长[{width}],宽[{height}]")except BaseException:text.set("")win.destroy()def choose_color(x):color = colorchooser.askcolor(title="颜色选择")if color[1]:x["background"] = color[1]def choose_font(x):ft = filedialog.askopenfilename(initialdir=r"C:\Windows\Fonts")if ft:x.set(ft)def visibleWaterMark(frame):markContent, phoPath, positionX, positionY, markFont, fontSize = tk.StringVar(), tk.StringVar(), tk.IntVar(), tk.IntVar(), tk.StringVar(), tk.IntVar()nameList = ['内容', '颜色', '图片路径', '左上角x', '左上角y', '字体', '字体大小']varList = [markContent, 1, phoPath, positionX, positionY, markFont, fontSize]markFont.set("STSONG.TTF")fontSize.set(20)positionX.set(10)positionY.set(10)imageSize = tk.StringVar()ttk.Label(frame, textvariable=imageSize).place(relx=.7, rely=.42)ch_color = tk.Button(frame, width=3, relief="groove", command=lambda: choose_color(ch_color), background="#000000", text="")for i in range(len(nameList)):ttk.Label(frame, text=nameList[i]).place(relx=.3, rely=.12 + i * .1)if i == 1:ch_color.place(relx=.4, rely=.12 + i * .1)continueif i == 5:ttk.Combobox(frame, textvariable=markFont, values=list(os.walk(r"C:\Windows\Fonts"))[0][2], width=17).place(relx=.4, rely=.12 + i * .1)continuetk.Entry(frame, textvariable=varList[i], width=20).place(relx=.4, rely=.12 + i * .1)if i == 2:ttk.Button(frame, text="浏览", width=8, command=lambda: visibleWaterMarkFindFile(varList[2], imageSize)).place(relx=.6, rely=.12 + i * .1)ttk.Button(frame, text="添加水印", width=8, command=lambda: watermark_Image(phoPath.get(), markContent.get(), pos=(positionX.get(), positionY.get()),color=ch_color["background"], textFont=markFont.get(), textSize=fontSize.get())).place(relx=.43, rely=.85)instruction = ("不支持图片保存;\n作者为了打游戏,并且希望用户可以有最大的自由去制作水印,位置x,y可以靠感觉做,默认左上角;\n""字体设置方法:打开[C:\Windows\Fonts],右键选择喜欢的字体,右键[属性],复制[*.ttf],粘贴至[字体]就完成设定了;\n""三个需要填入数字的输入框均只支持整数;\n")ttk.Button(frame, text="Help", width=8, command=lambda: messagebox.showinfo(title="提示信息", message=instruction)).place(relx=.9, rely=.8)ttk.Button(frame, text="Close", width=8, command=lambda: window.destroy()).place(relx=.9, rely=.9)if __name__ == '__main__':window = tk.Tk()window.title("VisibleWaterMark")screen_w, screen_h = window.winfo_screenwidth(), window.winfo_screenheight()w, h = int(screen_w / 2), int(screen_h / 2)window.geometry(f'{w}x{h}+{int(screen_w / 4)}+{int(screen_h / 4)}')window.resizable(False, False)# window.iconbitmap(".\\resource\\pg.ico")window.attributes('-alpha', 0.9)f = ttk.Frame(window)visibleWaterMark(f)f.place(relx=0, rely=0, width=w, height=h)window.mainloop()

分析

核心代码

def watermark_Image(img_path, text, pos, color="#FFFFFF", textFont="STSONG.TTF", textSize=20):image = Image.open(img_path)drawing = ImageDraw.Draw(image)# 加载字体文件font = ImageFont.truetype(font=textFont, size=textSize)drawing.text(pos, text, fill=color, font=font)image.show()# img.save(output_path)

浏览字体文件夹并提取字体

ttk.Combobox(frame, textvariable=markFont, values=list(os.walk(r"C:\Windows\Fonts"))[0][2], width=17  # 浏览字体文件夹并提取字体).place(relx=.4, rely=.12 + i * .1)

 选择颜色

def choose_color(x):color = colorchooser.askcolor(title="颜色选择")if color[1]:x["background"] = color[1]

 选择图片并给出图片的参数

def visibleWaterMarkFindFile(x, text):win = tk.Tk()win.withdraw()file_path = filedialog.askopenfilename()x.set(file_path)try:image = Image.open(file_path)width, height = image.sizetext.set(f"图片长[{width}],宽[{height}]")except BaseException:text.set("")win.destroy()

实现效果

UI

 结果

完成后会弹出图片,不会自动保存,需要用户自己手动保存,免得程序保存图片到一个用户找不到的位置


http://www.mrgr.cn/news/54556.html

相关文章:

  • leetcode22.括号生成
  • uoload-labs靶场Pass-09
  • 边界AIchat:智能办公与学习的全能助手
  • C++面试速通宝典——27
  • nginx过滤模块怎么生效的
  • 秒懂MVC, MVP, MVVM框架
  • 控制系统 状态观测问题 Kalman卡尔曼(附MATLAB实现)
  • 深入剖析:神经网络的结构与功能解读
  • Monorepo 管理多个包
  • SpringBoot项目整合Knife4J
  • Spark 同步 MySQL 数据到 Hive:技术实践与代码示例
  • Python内存管理入门:理解和优化你的代码
  • 【智能算法应用】徒步优化算法求解二维路径规划问题
  • Nature 正刊丨群体爆发中的神经元序列在人类皮层中编码信息
  • 房子,它或许是沃土
  • STM32传感器模块编程实践(七) MLX90614红外测温模块简介及驱动源码
  • Atlas800昇腾服务器(型号:3000)—CANN安装(二)
  • 【优选算法】探索双指针之美(一):双指针与单调性的完美邂逅
  • 从零开始学PHP之输出语句变量常量
  • 加减乘除计算指令整理
  • uniapp+vue3+uview-plus修改默认样式
  • d3dcompiler_43.dll丢失怎么修复?分享5种实用方法助轻松搞定
  • 有口才的从业者一定是位人才
  • Linux服务器安装SRAToolkit教程
  • 一通瞎写居然击败100%【力扣】【498-对角线遍历】【数组-C语言】
  • 文献分享: Vamana图算法以及面向SSD的DiskANN