尝试写了一个去除图片周围像素填充的函数,大概测试了一下,功能没啥问题,可以去除四边任意的像素值的填充,当然会先检查一下的,对于上下或者左右的边框去除,有两种模式,一种是可以去除两边不同像素值的,一个是去除限定相同像素值的。

不过测试的图片是四周都是[15,15,15]的填充

from PIL import Image, PngImagePlugin
import numpy as npdef crop_img_idex(mask, begin, stop, order = True):if order:sequen = 1else:sequen = -1id = 0  # 这里不先赋值 就会报错local variable 'id' referenced before assignmentfor id in range(begin, stop, sequen):if mask[id] > 0:breakreturn iddef whether_to_rm(mask, bottom = False):to_rm = Trueif not bottom:if mask[1] > 0:to_rm = Falseprint("无边框可以移除")elif bottom:if mask[-1] >0:to_rm =Falseprint("无边框可以移除")return to_rmdef remove_one_edge(Img, imarray, top = True, bottom = False, left = False, right = False, diff = False):# top 0 h-1 bottom h-1 0 left 0 w-1w = Img.width  # 图片的宽h = Img.height  # 图片的高w_begin, h_begin, w_new, h_new = 0, 0, w, hif top or left:judge = imarray[0, 0]elif bottom == 1 or right:judge = imarray[h - 1, w - 1]mask = np.sum(imarray != judge, axis=-1) > 0if top or bottom:mask1 = np.sum(mask, axis=-1)if not whether_to_rm(mask1, bottom=bottom):return Imgif top:h_begin = crop_img_idex(mask1, 0, h)h_begin = h_begin + 1print(f"将要剪裁上边界至像素{h_begin}")elif bottom:h_new = crop_img_idex(mask1, h-1, 0, order=False)h_new = h_new - 1print(f"将要剪裁下边界至像素{h_new}")if left or right:mask2 = np.sum(mask, axis=-2)if not whether_to_rm(mask2,bottom=right):return Imgif left:w_begin = crop_img_idex(mask2, 0, w)w_begin = w_begin + 1print(f"将要剪裁左边界至像素{w_begin}")elif right:w_new = crop_img_idex(mask2, w-1, 0,order=False)w_new = w_new - 1print(f"将要剪裁右边界至像素{w_new}")Img = Img.crop((w_begin, h_begin, w_new, h_new))return Imgdef rm_edges(Img,imarray,edge ):w = Img.width  # 图片的宽h = Img.height  # 图片的高w_begin, h_begin, w_new, h_new = 0, 0, w, hif edge.all == np.array([0,1,0,1]).all:judge = imarray[h - 1, w - 1]else:judge = imarray[0, 0]mask = np.sum(imarray != judge, axis=-1) > 0maskud = np.sum(mask, axis=-1)masklr = np.sum(mask, axis=-2)if edge[0]==1:h_begin = crop_img_idex(maskud, 0, h)h_begin = h_begin + 1print(f"将要剪裁上边界至像素{h_begin}")if edge[1]==1:h_new = crop_img_idex(maskud, h - 1, 0, order=False)h_new = h_new - 1print(f"将要剪裁下边界至像素{h_new}")if edge[2]==1:w_begin = crop_img_idex(masklr, 0, w)w_begin = w_begin + 1print(f"将要剪裁左边界至像素{w_begin}")if edge[3]==1:w_new = crop_img_idex(masklr, w - 1, 0, order=False)w_new = w_new - 1print(f"将要剪裁右边界至像素{w_new}")Img = Img.crop((w_begin, h_begin, w_new, h_new))return Imgdef remove_edge(img, edge = None, style = "diff"):"""img: 传入图片,格式为PIL格式,np格式,或者是路径edge: 为一个list[x,x,x,x] x为0或者1,如果为1则代表去除这条边的边框,顺序分别为上下左右,同时为1的两条边的边界可以是一样的也可以是不一样的,默认选项为diffstyle:["same", "diff"],same 表示去除的几个边的框框的像素值需要是一样的,diff表示只要框的一条边上像素值是一样的就可以去除当edge没传参的时候,默认按照上下左右顺序,尽可能去掉大宽度的边框"""if type(img) == str:img = Image.open(img)imarray = np.array(img)elif type(img) is np.ndarray:imarray = imgimg = Image.fromarray(imarray)elif type(img) is PngImagePlugin.PngImageFile:imarray = np.array(img)else:print("对象既不是地址,也不是数组,PngImageFile")return 0#print(type(edge))if type(edge) == list:edge = np.array(edge)print(  (type(edge) is np.ndarray and (edge.ndim != 1 or edge.shape != (4,))) )if (type(edge) is None) and (type(edge) is not np.ndarray  or (type(edge) is np.ndarray and (edge.ndim != 1 or edge.shape != (4,)))):print("edge 传入参数错误!")return imgif edge is not None and sum(edge) != sum(edge == 1):# 修正edge的数值for i in [0, 1, 2, 3]:if edge[i]<0:edge[i] = 0elif edge[i]>0:edge[i] = 1if edge is not None and sum(edge) == 0:print("无需修改")return imgw = img.width  # 图片的宽h = img.height  # 图片的高if edge is not None and sum(edge) == 1:# 这个情况下 无论是diff还是same都是一样的img = remove_one_edge(img, imarray, edge[0]==1, edge[1]==1, edge[2]==1, edge[3]==1)print(1)elif edge is not None and sum(edge) == 2:edge1 = edge.reshape(2, 2)if np.sum((np.sum(edge1, axis=-1) == 2)):# 这里是对边 对边是区分diff和 same的print("这里是对边")if style == "diff":img = remove_one_edge(img, imarray, edge[0]==1, False, edge[2]==1, False)imarray = np.array(img)img = remove_one_edge(img, imarray, False, edge[1] == 1, False, edge[3] == 1)elif style == "same":img = rm_edges(img, imarray, edge)elif edge is not None and np.sum((np.sum(edge, axis=-1) == 2)):#这里是临边 临边其实不区分print("这里是临边")img = rm_edges(img, imarray, edge)elif edge is not None and sum(edge) >= 2:img = rm_edges(img, imarray, edge)elif edge == None:img = remove_one_edge(img, imarray, True, False, True, False)imarray = np.array(img)img = remove_one_edge(img, imarray, False, True, False, True)return img
path = "E:\\docu\\QQ\\file\\frame000.png"
img = Image.open(path)
#imarray = np.array(img)
# type(imarray)  imarray.shape imarray.ndim
img = remove_edge(img, [1,0,1,0])
print(img.width, img.height)
img.show()

去除图片周围的像素填充相关推荐

  1. Photoshop去除图片水印

    环境:Adobe Photoshop CC 2019 1.首先使用ps打开需要操作的图片,command+j 复制图层 2.点击矩形选框工具 ,框中需要去除的水印 3.选中色彩范围项,用取色工具(鼠标 ...

  2. 快速批量去除图片水印方法大全~~

    原文地址:http://blog.163.com/simonyao_cool/blog/static/16512555720107311858809/ 去水印是不复杂啦,可几千页下来,自己用橡皮抹会抹 ...

  3. 怎么去水印不破坏原图?这4个方法,无痕去除图片水印

    平时在网上下载图片的时候,经常会有带水印的情况,怎么才能去掉图片的水印呢?今天就给大家分享4个好用的图片去水印工具,操作简单使用方便,主要是去水印效果好,去水印后几乎看不出水印痕迹! 1.imgcle ...

  4. 去除图片水印_只需一键,即可快速去除图片水印!如此简单的方法,谁不知谁吃亏...

    现在有很多小伙伴都会到网上找一些好看的图片保存下来,但是很多时候我们找到的图片都是带有水印的,虽然水印很小,但是看起来也是怪怪的.别担心,今天小编将分享几个简单又好用的图片去水印方法给大家,有需要的小 ...

  5. html怎么去除照片背景颜色,photoshop怎么去除图片背景色

    在我们的日常生活中,图片是必不可少的一项信息和工具,在使用图片时,难免会遇到需要处理的图片,那么大家知道photoshop怎么去除图片背景色吗?下面是学习啦小编整理的photoshop怎么去除图片背景 ...

  6. 如何去除图片上的文字(PS使用教程)

    很多时候由于工作的需要,需要对我们的图片进行修改,修改的同时还想要保存我们的图片背景,所以很多人就不知道怎么弄了,小编跟大家分享一下使用PS如何简单的去掉图片上的文字,希望对大家有所帮助! 方法/步骤 ...

  7. python-opencv去除小面积区域/孔洞填充(二值图像)

    上图左为原始图像,右图为填充后的图像. 因为左边的图片存在很多噪声点,直接根据阈值去填充会存在问题,所以我就先对图片进行了一次二值化处理,然后调用了opencv的fillPoly函数完成孔洞的填充. ...

  8. Python OpenCV去除图片水印

    问题描述 去除百度水印 1.jpg,背景简单 2.jpg,背景复杂 解决方案 安装 pip install opencv-python pip install numpy 方法一.OpenCV提取颜色 ...

  9. 教你一招利用Python快速去除图片水印

    大家好,我是IT界搬运工. 相信大家都有在网上下载好图片但是有水印的烦恼,那么问题就来了:看到心爱的图片想要"占为己有".怎么把图片上的水印去除呢?今天我就来教你一招利用Pytho ...

最新文章

  1. 【转载】邻接表表示法
  2. RHEL5 下使用syslog-ng构建集中型日志服务器
  3. linux的tmp目录不会清空,关于Linux系统中/tmp目录的清除问题
  4. php代码在线快速生成,PHPGEN在线代码生成器
  5. ae绘图未指定错误怎么办_早晨深化设计研究院47个快捷键50个CAD技巧助你神速绘图,玩转CAD...
  6. 计算机准考证打印山东省招生教育,山东省教育招生考试院2020高考准考证打印入口:wsbm.sdzk.cn...
  7. OpenGL运用辅佐库创立规矩几许目标
  8. Java NIO_I/O基本概念_Java中的缓冲区(Buffer)_通道(Channel)_网络I/O
  9. 分配和释放 BSTR 的内存
  10. 微型计算机原理第二版学习辅导,微型计算机原理及应用学习辅导.pdf
  11. mysql增加列并增加comment_运城多条高铁线路调整动车列数增加到99列
  12. Win7下使用VC6.0的注意事项
  13. html5 fa图标库,轻松学会在项目中使用fontawesome字体图标
  14. 图文讲解:Win8必知快捷键汇总_-Chaz-_新浪博客
  15. Gstore官网学习一:知识图谱与gStore介绍
  16. 华为nova7se乐活版支持鸿蒙,华为nova7se乐活版和nova8se的详细对比参数对比
  17. Linux小白的大师之路
  18. JsonParser
  19. Java单元测试工具:JUnit4(一)——概述及简单例子
  20. Joint Model (Intent+Slot)

热门文章

  1. 解决360\小红伞杀毒报网页HTML.Rce.Gen3恶意程序的问题
  2. nginx: [warn] the user directive makes sense only if the master process runs with super-user privi
  3. RK3568处理器体验小记
  4. 世界上最经典的感情短语
  5. bootstrap徽章颜色
  6. 计算机二级证书免费发到学校,计算机二级证书去哪里领取
  7. Bugku-ping
  8. 2022邮箱如何发送群发邮件?邮件群发平台软件哪个好?一文get群发小技巧
  9. 为网页添加样式(选择器和声明块)
  10. Linux 下的 /proc 目录介绍