用python按照图像灰度值统计并筛选图片的操作(PIL,shutil,os)


Posted in Python onJune 04, 2020

我就废话不多说了,大家还是直接看代码吧!

import PIL.Image
import numpy
import os
import shutil
def sum_right(path):
 img = PIL.Image.open(path)
 array = numpy.array(img)
 num = array.sum(axis=0)
 print(type(num))
 res_left = 0
 res_right = 0
 for i in range(256,512):
  res_right += num[i]
 print(res_right)

if __name__ == '__main__':
 dir2 = r"C:\Users\Howsome\Desktop\tst"
 dir1 = r"C:\Users\Howsome\Desktop\AB"
 names = os.listdir(dir1)
 n = len(names)
 print("文件数量",n)
 res = 0
 average_5 = 25565356
 average_25 = 26409377
 average_5_right = 10006019
 #average_tmp = (average_25+average_5)//2
 count = 0
 #show(os.path.join(dir1, "uni4F6C.png"))
 for i in range(n):
  #取图片
  img = PIL.Image.open(os.path.join(dir1,names[i]))
  file = os.path.join(dir1,names[i])
  rmfile = os.path.join(dir2,names[i])
  array = numpy.array(img)
  num = array.sum(axis=0)
  res_right = 0
  for i in range(256, 512):
   res_right += num[i]
  average_5_right += res_right/n

  if res_right > average_5_right:
    shutil.copyfile(file, rmfile)
    os.remove(file)
    count += 1
 print(average_5_right)
 print(count)

补充知识:python遍历灰度图像像素方法总结

啥也不说了,看代码吧!

import numpy as np
import matplotlib.pyplot as plt
import cv2
import time

img = cv2.imread('lena.jpg',0)

# 以遍历每个像素取反为例

# 方法1
t1 = time.time()
img1 = np.copy(img)
rows,cols = img1.shape[:2]
for row in range(rows):
 for col in range(cols):
  img[row,col] = 255 - img[row,col]
t2 = time.time()
print('方法1所需时间:',t2-t1)

# 方法2
t3 = time.time()
img2 = np.copy(img)
rows,cols = img2.shape[:2]
img2 = img2.reshape(rows*cols)
# print(img2)
for i in range(rows*cols):
 img2[i] = 255-img2[i]
img2 = img2.reshape(rows,cols)
# print(img2)
t4 = time.time()
print('方法2所需时间:',t4-t3)

# 方法3
t5 = time.time()
img3 = np.copy(img)
# 使用多维迭代生成器
for (x,y), pixel in np.ndenumerate(img3):
 img3[x,y] = 255-pixel
t6 = time.time()
print('方法3所需时间:',t6-t5)

测试结果:

方法1所需时间: 0.14431977272033691
方法2所需时间: 0.13863205909729004
方法3所需时间: 0.24196243286132812

以上这篇用python按照图像灰度值统计并筛选图片的操作(PIL,shutil,os)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
在Python中使用第三方模块的教程
Apr 27 Python
在Python中编写数据库模块的教程
Apr 29 Python
Python ftp上传文件
Feb 13 Python
解决pyinstaller打包exe文件出现命令窗口一闪而过的问题
Oct 31 Python
在IPython中执行Python程序文件的示例
Nov 01 Python
对Python中的条件判断、循环以及循环的终止方法详解
Feb 08 Python
python连接mongodb集群方法详解
Feb 13 Python
Python Flask上下文管理机制实例解析
Mar 16 Python
Python实现Word表格转成Excel表格的示例代码
Apr 16 Python
pip安装tensorflow的坑的解决
Apr 19 Python
python用海龟绘图写贪吃蛇游戏
Jun 18 Python
Python docx库删除复制paragraph及行高设置图片插入示例
Jul 23 Python
Python flask框架实现浏览器点击自定义跳转页面
Jun 04 #Python
python 图像判断,清晰度(明暗),彩色与黑白实例
Jun 04 #Python
完美解决ARIMA模型中plot_acf画不出图的问题
Jun 04 #Python
Python使用Matlab命令过程解析
Jun 04 #Python
Python flask框架端口失效解决方案
Jun 04 #Python
Python实现列表中非负数保留,负数转化为指定的数值方式
Jun 04 #Python
Python新手学习装饰器
Jun 04 #Python
You might like
php学习 函数 课件
2008/06/15 PHP
给初学者的30条PHP最佳实践(荒野无灯)
2011/08/02 PHP
php curl请求信息和返回信息设置代码实例
2015/04/27 PHP
php curl抓取网页的介绍和推广及使用CURL抓取淘宝页面集成方法
2015/11/30 PHP
php mysql procedure实现获取多个结果集的方法【基于thinkPHP】
2016/11/09 PHP
JavaScript 学习笔记之一jQuery写法图片等比缩放以及预加载
2012/06/28 Javascript
Jquery选中或取消radio示例
2013/09/29 Javascript
Array栈方法和队列方法的特点说明
2014/01/24 Javascript
常用原生js自定义函数总结
2016/11/20 Javascript
javascript函数的四种调用模式
2017/01/08 Javascript
ES6新特性之变量和字符串用法示例
2017/04/01 Javascript
JS高级技巧(简洁版)
2018/07/29 Javascript
微信小程序实现动态获取元素宽高的方法分析
2018/12/10 Javascript
uni-app 组件里面获取元素宽高的实现
2019/12/27 Javascript
Python中让MySQL查询结果返回字典类型的方法
2014/08/22 Python
使用Python的Tornado框架实现一个Web端图书展示页面
2016/07/11 Python
简单谈谈Python中的闭包
2016/11/30 Python
Vue的el-scrollbar实现自定义滚动
2018/05/29 Python
python实现搜索文本文件内容脚本
2018/06/22 Python
Python延时操作实现方法示例
2018/08/14 Python
使用pycharm设置控制台不换行的操作方法
2019/01/19 Python
Python Django基础二之URL路由系统
2019/07/18 Python
Pycharm调试程序技巧小结
2020/08/08 Python
荷兰鞋子在线:Nelson Schoenen
2017/12/25 全球购物
日本最大的购物网站:日本乐天市场(Rakuten Ichiba)
2020/11/04 全球购物
英国鲜花递送:Blossoming Gifts
2020/07/10 全球购物
幼儿园运动会加油词
2014/02/14 职场文书
老公爱的承诺书
2014/03/31 职场文书
捐款倡议书怎么写
2014/05/13 职场文书
银行业务授权委托书
2014/10/10 职场文书
高中生综合素质评价范文
2015/08/18 职场文书
感谢信的技巧及范例
2019/05/15 职场文书
MySQL 分组查询的优化方法
2021/05/12 MySQL
使用Vue3+Vant组件实现App搜索历史记录功能(示例代码)
2021/06/09 Vue.js
Java详细解析==和equals的区别
2022/04/07 Java/Android
电脑开机弹出documents文件夹怎么回事?弹出documents文件夹解决方法
2022/04/08 数码科技