- #!/usr/bin/env python
- # -*- encoding: utf-8 -*-
- '''
- [url=home.php?mod=space&uid=267492]@file[/url] : RWM.py
- [url=home.php?mod=space&uid=238618]@Time[/url] : 2021/11/25 19:25:55
- [url=home.php?mod=space&uid=686208]@AuThor[/url] : Ljujl
- [url=home.php?mod=space&uid=1248337]@version[/url] : 1.0
- @Contact : [url=mailto:mr_liu133299@foxmail.com]mr_liu133299@foxmail.com[/url]
- '''
-
- # here put the import lib
-
- from os import path
- from tkinter import (BOTH, BROWSE, EXTENDED, INSERT, Button, Frame, Label,
- Text, Tk, filedialog, mainloop, messagebox)
- from PIL import Image, ImageTk
-
-
- class Remove_watermark():
- def __init__(self) -> None:
-
- self.root = Tk()
- self.root.title("去水印大师")
- x = (self.root.winfo_screenwidth() - self.root.winfo_reqwidth()) // 4
- y = (self.root.winfo_screenheight() - self.root.winfo_reqheight()) // 4
- self.root.geometry(f"{x}x{y}")
- self.frame = Frame(self.root).grid(row=0, column=0)
- self.old_pic_frame = Frame(self.root).grid(row=1, column=0)
- self.new_pic_frame = Frame(self.root).grid(row=1, column=1)
- self.width = 10
- btn_open = Button(self.frame, text="打开图片", width=self.width, height=1, command=self.open_pic,).grid(
- row=0, column=0) #
- label_white = Label(self.frame, text="", height=1, width=self.width).grid(
- row=0, column=1)
- btn_process = Button(self.frame, text="角落水印", width=self.width, height=1, command=self.process,).grid(
- row=0, column=2) #
- label_white = Label(self.frame, text="", height=1, width=self.width).grid(
- row=0, column=3)
- btn_process = Button(self.frame, text="文档水印", width=self.width,
- height=1, command=self.process_all,).grid(row=0, column=4)
-
- def open_pic(self):
- global img
- self.screenwidth = self.root.winfo_screenwidth()
- self.screenheight = self.root.winfo_screenheight()
- self.root.geometry(
- f"{self.screenwidth}x{self.screenheight}+0+0")
- self.filepath = filedialog.askopenfilename(title='选择图片', filetypes=[
- ('图片', ['*.jpg', '*.png', '*.gif'])])
- img_open = Image.open(fp=self.filepath).convert("RGB")
- self.img_width, self.img_height = img_open.size
- print(self.img_width, self.img_height)
- self.rate = self.img_width / self.img_height
- # 如果图片高度过高则进行缩小
- if self.img_height > self.screenheight * 0.5:
- width = int(0.5 * self.screenwidth)
- height = int(width / self.rate)
- img = ImageTk.PhotoImage(
- image=img_open.resize(size=(width, height)))
- else:
- img = ImageTk.PhotoImage(img_open)
- label_img = Label(self.old_pic_frame, image=img).grid(row=1, column=1)
-
- def process(self):
- """处理右下角水印"""
- global new_img
- im = Image.open(self.filepath).convert("RGB")
- right_bottom = 4 # 右下角水印位置
- for w in range(self.img_width//4, self.img_width):
- for h in range(self.img_height//4, self.img_height):
- pos = (w, h)
- if sum(im.getpixel(pos)[:3]) > 600:
- im.putpixel(pos, (255, 255, 255))
- new_pic_path = path.dirname(
- self.filepath) + "/去水印_" + path.basename(self.filepath)
- im.save(new_pic_path)
- img_open = Image.open(fp=new_pic_path)
- # 如果图片高度过高则进行缩小
- if self.img_height > self.screenheight * 0.5:
- width = int(0.5 * self.screenwidth)
- height = int(width / self.rate)
- new_img = ImageTk.PhotoImage(
- image=img_open.resize(size=(width, height)))
- else:
- new_img = ImageTk.PhotoImage(img_open)
-
- label_img_new = Label(self.new_pic_frame, image=new_img).grid(
- row=1, column=2)
- messagebox.showinfo('温馨提示', "完成去除水印啦(:")
-
- def process_all(self):
- global new_img
- im = Image.open(self.filepath).convert("RGB")
- width, height = im.size
-
- for w in range(width):
- for h in range(height):
- pos = (w, h)
- r, g, b = im.getpixel(pos)[:3]
- avg = (r + g + b) / 3
- limit = 0.10
- if avg > 100 and abs((r-avg)/avg) < limit and abs((g-avg)/avg) < limit and abs((b-avg)/avg) < limit:
- im.putpixel(pos, (255, 255, 255))
- new_pic_path = path.dirname(
- self.filepath) + "/去水印_" + path.basename(self.filepath)
- im.save(new_pic_path)
- img_open = Image.open(fp=new_pic_path).convert("RGB")
-
- # 如果图片高度过高则进行缩小
- if self.img_height > self.screenheight * 0.5:
- width = int(0.5 * self.screenwidth)
- height = int(width / self.rate)
- new_img = ImageTk.PhotoImage(
- image=img_open.resize(size=(width, height)))
- else:
- new_img = ImageTk.PhotoImage(img_open)
- label_img_new = Label(
- self.new_pic_frame, image=new_img).grid(row=1, column=2) # , columnspan=3,sticky="EW",
- messagebox.showinfo('温馨提示', "完成去除水印啦(:")
-
-
- if __name__ == "__main__":
- main = Remove_watermark()
- img = None
- new_img = None
- mainloop()
Python一键去除图片水印代码
来源:网络
发布人:天道酬勤
发布时间:2024-02-15
免责声明:文中图文均系网友发布,转载来自网络,如有侵权请联系右侧客服QQ删除,无忧屋网友发布此文仅为传递信息,不代表无忧屋平台认同其观点。
热门文章
1
PearDownloader.js一款能下载加速的js组件
2workerman 内网部署外网访问的方法
3python open utf8格式
4thinkphp8 查询的时候 where in 是用哪个
5js指定时间定时自动执行代码分享
6PHP获取文件体积大小+体积单位字节
7php如何实现$GLOBALS赋值?
8js随机生成指定位数+数字+字母混合字符串
9java 循环给rbmq发送消息 需要关闭连接吗
10PHP实现随机一句话留言教程
11jQuery获取file控件中图片的宽高与大小
12python发送邮件示例代码
13workerman 内网部署外网访问的配置方法
14java单例模式为什么一定要构造器私有?
15使用PHP获取XHR请求来源方法
16is_dir()函数判断目录是否存在方法
17SQL查询统计内容相同的字段出现次数
18MySQL导入数据库报错"Unknown character set: 'utf8mb4'"
19java中的面向对象机制常用类与异常处理机制
20php获取请求来源域名的四种方法
21SQL清空全部数据表内容,并且ID重新从1开始
22利用openssl函数加密数据来POST传递数据
23Python编写zimg 存储在ssdb中的图片如何本地化
24JS查询页面是否被百度收录,没有收录则显示搜索链接
25python **代表什么意思