当前位置: 无忧屋首页 > 文章中心 > Python >

Python一键去除图片水印代码

来源:网络

发布人:天道酬勤

发布时间:2024-02-15

  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. [url=home.php?mod=space&uid=267492]@file[/url]    :   RWM.py
  5. [url=home.php?mod=space&uid=238618]@Time[/url]    :   2021/11/25 19:25:55
  6. [url=home.php?mod=space&uid=686208]@AuThor[/url]  :   Ljujl
  7. [url=home.php?mod=space&uid=1248337]@version[/url] :   1.0
  8. @Contact :   [url=mailto:mr_liu133299@foxmail.com]mr_liu133299@foxmail.com[/url]
  9. '''
  10.   
  11. # here put the import lib
  12.   
  13. from os import path
  14. from tkinter import (BOTH, BROWSE, EXTENDED, INSERT, Button, Frame, Label,
  15.                      Text, Tk, filedialog, mainloop, messagebox)
  16. from PIL import Image, ImageTk
  17.   
  18.   
  19. class Remove_watermark():
  20.     def __init__(self) -> None:
  21.   
  22.         self.root = Tk()
  23.         self.root.title("去水印大师")
  24.         x = (self.root.winfo_screenwidth() - self.root.winfo_reqwidth()) // 4
  25.         y = (self.root.winfo_screenheight() - self.root.winfo_reqheight()) // 4
  26.         self.root.geometry(f"{x}x{y}")
  27.         self.frame = Frame(self.root).grid(row=0, column=0)
  28.         self.old_pic_frame = Frame(self.root).grid(row=1, column=0)
  29.         self.new_pic_frame = Frame(self.root).grid(row=1, column=1)
  30.         self.width = 10
  31.         btn_open = Button(self.frame, text="打开图片", width=self.width, height=1, command=self.open_pic,).grid(
  32.             row=0, column=0)  #
  33.         label_white = Label(self.frame, text="", height=1, width=self.width).grid(
  34.             row=0, column=1)
  35.         btn_process = Button(self.frame, text="角落水印", width=self.width, height=1, command=self.process,).grid(
  36.             row=0, column=2)  #
  37.         label_white = Label(self.frame, text="", height=1, width=self.width).grid(
  38.             row=0, column=3)
  39.         btn_process = Button(self.frame, text="文档水印", width=self.width,
  40.                              height=1, command=self.process_all,).grid(row=0, column=4)
  41.   
  42.     def open_pic(self):
  43.         global img
  44.         self.screenwidth = self.root.winfo_screenwidth()
  45.         self.screenheight = self.root.winfo_screenheight()
  46.         self.root.geometry(
  47.             f"{self.screenwidth}x{self.screenheight}+0+0")
  48.         self.filepath = filedialog.askopenfilename(title='选择图片', filetypes=[
  49.             ('图片', ['*.jpg', '*.png', '*.gif'])])
  50.         img_open = Image.open(fp=self.filepath).convert("RGB")
  51.         self.img_width, self.img_height = img_open.size
  52.         print(self.img_width, self.img_height)
  53.         self.rate = self.img_width / self.img_height
  54.         # 如果图片高度过高则进行缩小
  55.         if self.img_height > self.screenheight * 0.5:
  56.             width = int(0.5 * self.screenwidth)
  57.             height = int(width / self.rate)
  58.             img = ImageTk.PhotoImage(
  59.                 image=img_open.resize(size=(width, height)))
  60.         else:
  61.             img = ImageTk.PhotoImage(img_open)
  62.         label_img = Label(self.old_pic_frame, image=img).grid(row=1, column=1)
  63.   
  64.     def process(self):
  65.         """处理右下角水印"""
  66.         global new_img
  67.         im = Image.open(self.filepath).convert("RGB")
  68.         right_bottom = 4  # 右下角水印位置
  69.         for w in range(self.img_width//4, self.img_width):
  70.             for h in range(self.img_height//4, self.img_height):
  71.                 pos = (w, h)
  72.                 if sum(im.getpixel(pos)[:3]) > 600:
  73.                     im.putpixel(pos, (255, 255, 255))
  74.         new_pic_path = path.dirname(
  75.             self.filepath) + "/去水印_" + path.basename(self.filepath)
  76.         im.save(new_pic_path)
  77.         img_open = Image.open(fp=new_pic_path)
  78.         # 如果图片高度过高则进行缩小
  79.         if self.img_height > self.screenheight * 0.5:
  80.             width = int(0.5 * self.screenwidth)
  81.             height = int(width / self.rate)
  82.             new_img = ImageTk.PhotoImage(
  83.                 image=img_open.resize(size=(width, height)))
  84.         else:
  85.             new_img = ImageTk.PhotoImage(img_open)
  86.   
  87.         label_img_new = Label(self.new_pic_frame, image=new_img).grid(
  88.             row=1, column=2)
  89.         messagebox.showinfo('温馨提示', "完成去除水印啦(:")
  90.   
  91.     def process_all(self):
  92.         global new_img
  93.         im = Image.open(self.filepath).convert("RGB")
  94.         width, height = im.size
  95.   
  96.         for w in range(width):
  97.             for h in range(height):
  98.                 pos = (w, h)
  99.                 r, g, b = im.getpixel(pos)[:3]
  100.                 avg = (r + g + b) / 3
  101.                 limit = 0.10
  102.                 if avg > 100 and abs((r-avg)/avg) < limit and abs((g-avg)/avg) < limit and abs((b-avg)/avg) < limit:
  103.                     im.putpixel(pos, (255, 255, 255))
  104.         new_pic_path = path.dirname(
  105.             self.filepath) + "/去水印_" + path.basename(self.filepath)
  106.         im.save(new_pic_path)
  107.         img_open = Image.open(fp=new_pic_path).convert("RGB")
  108.   
  109.         # 如果图片高度过高则进行缩小
  110.         if self.img_height > self.screenheight * 0.5:
  111.             width = int(0.5 * self.screenwidth)
  112.             height = int(width / self.rate)
  113.             new_img = ImageTk.PhotoImage(
  114.                 image=img_open.resize(size=(width, height)))
  115.         else:
  116.             new_img = ImageTk.PhotoImage(img_open)
  117.         label_img_new = Label(
  118.             self.new_pic_frame, image=new_img).grid(row=1, column=2)  # , columnspan=3,sticky="EW",
  119.         messagebox.showinfo('温馨提示', "完成去除水印啦(:")
  120.   
  121.   
  122. if __name__ == "__main__":
  123.     main = Remove_watermark()
  124.     img = None
  125.     new_img = None
  126.     mainloop()

免责声明:文中图文均系网友发布,转载来自网络,如有侵权请联系右侧客服QQ删除,无忧屋网友发布此文仅为传递信息,不代表无忧屋平台认同其观点。