吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6185|回复: 62
收起左侧

[Python 转载] 【Python】一键去除图片水印,含简单可视化界面

   关闭 [复制链接]
mrliu133 发表于 2021-11-25 19:29
本帖最后由 mrliu133 于 2021-11-25 19:34 编辑

[Python] 纯文本查看 复制代码
#!/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()
界面3.png
界面1.png
界面4.png
界面2.png

1.微博图片水印

1.微博图片水印

2.微博图片去除水印之后

2.微博图片去除水印之后

3.文档图片水印

3.文档图片水印

4.文档图片去除水印

4.文档图片去除水印

免费评分

参与人数 12吾爱币 +10 热心值 +10 收起 理由
yg226 + 1 + 1 用心讨论,共获提升!
紫冰寒 + 1 + 1 谢谢@Thanks!
wanshiz + 1 + 1 热心回复!
Rv^ + 1 谢谢@Thanks!
xiahhhr + 1 + 1 谢谢@Thanks!
xhj666 + 1 + 1 热心回复!
LSLSLS + 1 热心回复!
shane2021 + 1 谢谢@Thanks!
lfm333 + 1 + 1 谢谢@Thanks!
cdwdz + 1 + 1 谢谢@Thanks!
小帥 + 1 下载地址???
LoveHack + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

涛之雨 发表于 2021-11-25 23:11
本帖最后由 涛之雨 于 2021-11-25 23:12 编辑
【Chrome插件】Chrome插件修改教程(一款GitHub的插件为例,附样品)
https://www.52pojie.cn/thread-1215596-1-1.html
(出处: 吾爱破解论坛)

我这个帖子里的水印应该不容易完全去掉吧。。。
我原图丢了,现在需要用到的时候我面对满屏的水印我不知所措。。。
 楼主| mrliu133 发表于 2021-11-26 09:21
知心 发表于 2021-11-26 09:19
你用pipenv进行打包,只安装需要用到的库

已经搞定,打包成功了,感谢你的建议
cxincn 发表于 2021-11-25 19:32
carrot2017 发表于 2021-11-25 19:40
学习学习,谢谢分享
Sugar01 发表于 2021-11-25 19:40
没看到在哪里下载啊?
buy360 发表于 2021-11-25 19:41
执行文件哪里哈
 楼主| mrliu133 发表于 2021-11-25 19:42
Sugar01 发表于 2021-11-25 19:40
没看到在哪里下载啊?

打包文件体积太大,还没降下来。你可以自己copy去打包。
 楼主| mrliu133 发表于 2021-11-25 19:44
buy360 发表于 2021-11-25 19:41
执行文件哪里哈

执行文件体积我还没搞定怎么让他变小些,希望有其他大佬优化一下吧。
Adgerlee 发表于 2021-11-25 20:04
大佬欸!!!
小帥 发表于 2021-11-25 20:31
本帖最后由 小帥 于 2021-11-25 20:34 编辑

没成品吗?
开心长寿果 发表于 2021-11-25 20:39
谢谢分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-28 18:14

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表