菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

Python实现图片水印去除的简单代码

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 图片去水印 Python源码

要使用Python一键去除图片水印,可以使用`pytesseract`和`PIL`库。首先需要安装这两个库: ```bash pip install pytesseract pip install pillow ``` 然后,可以使用以下代码去除图片水印: ```python from PIL import Image import pytesseract def remove_watermark(image_path, output_path): # 打开图片 image = Image.open(image_path) # 使用pytesseract识别图片中的文本 text = pytesseract.image_to_string(image) # 将识别到的文本写入新的图片 new_image = Image.new('RGB', (image.width, image.height), (255, 255, 255)) new_image.paste(image, (0,

#!/usr/bin/envpython#-*-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/2519: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]'''#hereputtheimportlibfromosimportpathfromtkinterimport(BOTH,BROWSE,EXTENDED,INSERT,Button,Frame,Label,Text,Tk,filedialog,mainloop,messagebox)fromPILimportImage,ImageTkclassRemove_watermark():def__init__(self)->None:self.root=Tk()self.root.title("去水印大师")x=(self.root.winfo_screenwidth()-self.root.winfo_reqwidth())//4y=(self.root.winfo_screenheight()-self.root.winfo_reqheight())//4self.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=10btn_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)defopen_pic(self):globalimgself.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.sizeprint(self.img_width,self.img_height)self.rate=self.img_width/self.img_height#如果图片高度过高则进行缩小ifself.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)defprocess(self):"""处理右下角水印"""globalnew_imgim=Image.open(self.filepath).convert("RGB")right_bottom=4#右下角水印位置forwinrange(self.img_width//4,self.img_width):forhinrange(self.img_height//4,self.img_height):pos=(w,h)ifsum(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)#如果图片高度过高则进行缩小ifself.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('温馨提示',"完成去除水印啦(:")defprocess_all(self):globalnew_imgim=Image.open(self.filepath).convert("RGB")width,height=im.sizeforwinrange(width):forhinrange(height):pos=(w,h)r,g,b=im.getpixel(pos)[:3]avg=(r+g+b)/3limit=0.10ifavg>100andabs((r-avg)/avg)<limitandabs((g-avg)/avg)<limitandabs((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")#如果图片高度过高则进行缩小ifself.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=Nonenew_img=Nonemainloop()


相关专题
评论
建站知识
建站知识
使用技巧
调试安装
运营推广