欢迎光临 - 我的站长站,本站所有资源仅供学习与参考,禁止用于商业用途或从事违法行为!

python教程

Python一键去除图片水印代码

python教程 我的站长站 2021-11-26 共82人阅读
#!/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()


相关专题
去水印
去水印
2021-05-13 222

去水印专题为您整理收藏了全方面去水印软件工具,包含去水印软件,去水印工具,去水印源码,还有热门的抖音去水印,快手去水印,安卓去水印等热门短视频去水印....

相关推荐
  • 图片去水印
  • Python源码
  • Free Canvas一键LOGO海报去水印下载插件v4.1.1
    Free Canvas一键LOGO海报去水印下载插件v4.1.1

    插件介绍Free Canvas是一款Chrome浏览器扩展插件,一键快速、免费下载某小智和某智客两个网站的高清无水印海报、LOGO、banner等素材,还支持自动裁剪功能,方便用户快速获得符合需求的素材。某小智和...

    浏览器插件 18 7个月前
  • 安卓无痕去水印小工具
    安卓无痕去水印小工具

    软件介绍安卓无痕去水印小工具是一款手机端强大的水印工具,支持一键去除视频、图片水印,一键为视频、图片增加水印。亲测去视频水印效果不错,加水印也不错,视频加水印直接贯穿整个视频!安卓去水印软件...

    软件分享 176 3年前
  • 一款自用的一键去除水印工具
    一款自用的一键去除水印工具

    一款自用的一键去除水印工具,找了好多去水印工具,这款真的非常好用,我的站长站推荐大家使用。除水印效果刚刚的,最主要的还简单小巧。只需要框选水印,而后去除即可。...

    软件分享 423 4年前
  • Photo StampP图片去水印便携版
    Photo StampP图片去水印便携版

    图片去水印软件介绍Photo StampP图片去水印便携版是一款功能强大的图片处理软件,单文件解压版,方便携带,免安装。有了这款软件可以非常轻松的将图片上的水印进行去除,除此之外,你还可以去除一些图片上...

    软件分享 211 3年前
  • Inpaint v9.0图片去水印绿色免安装版
    Inpaint v9.0图片去水印绿色免安装版

    软件介绍Inpaint v9.0是一款专业的去图片水印/瑕疵软件,可以从图片中去除不想要的部分,让您轻松摆脱图片上的水印、划痕、污渍、标志等瑕疵。使用魔术笔选择图片中不想要的区域(如额外的线、人物、...

    开发软件 122 3年前
  • Json压缩和格式化工具,附Python源码
    Json压缩和格式化工具,附Python源码

    软件介绍一款Json压缩和格式化工具,可以在线Json压缩和格式化。基于Python库开发,附上Python源码,GUI没有美化,巨丑。软件截图Python源码import jsonimport tkinter as tkdef json_compress(json_str...

    开发软件 34 10个月前
  • python打飞机小游戏源码+成品打包

    python源码用的pygame库,自带的random和os。程序运行需要的图片,声音和字体下载链接: https://pan.baidu.com/s/1KItG2usXOM_xcxcdHIixaw 提取码: qmweimport pygameimport randomimport os FPS = 60WIDTH = 500HEIGHT = 600 BLACK = (0, 0, 0)WHITE =...

    python教程 39 10个月前
  • 原创力文库Python爬虫下载源码

    # !/usr/bin/python# -*- coding: UTF-8 -*-import reimport jsonimport osimport shutilimport sysimport timeimport requestsimport img2pdffrom PIL import Image from alive_progress import alive_barfrom requests.exceptions import SSLErro...

    python教程 53 1年前
  • 讯飞听见语音转文字python源码

    讯飞听见语音转文字python源码,这个只能 转中文和英文,免费的转换不能超过3分钟。# -*- coding: utf-8 -*-# ☯ Author: ******# ☯ Email : ******@****.***# ☯ Date : 2021/06/24 20:13import osimport reimport timeimport randomimport logging...

    python教程 64 1年前
  • Python视频逐帧提取工具源码

    代码说明可以直接提取视频每一帧截图,只分享Python代码,自行打包。使用方法:将视频拉至窗口即可逐帧提取图像,默认生成在视频同目录下,效果看图。视频提取演示Python代码import osimport cv2import windndfrom tkinter import * def video_to_imgs(source...

    python教程 55 2年前