用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入门篇之数字
Oct 20 Python
浅要分析Python程序与C程序的结合使用
Apr 07 Python
K-means聚类算法介绍与利用python实现的代码示例
Nov 13 Python
关于django 1.10 CSRF验证失败的解决方法
Aug 31 Python
python 实现二维列表转置
Dec 02 Python
Python表达式的优先级详解
Feb 18 Python
详解Python 实现 ZeroMQ 的三种基本工作模式
Mar 24 Python
解决reload(sys)后print失效的问题
Apr 25 Python
小 200 行 Python 代码制作一个换脸程序
May 12 Python
python 3.8.3 安装配置图文教程
May 21 Python
OpenCV+Python3.5 简易手势识别的实现
Dec 21 Python
python中的列表和元组区别分析
Dec 30 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 autoload与spl_autoload自动加载机制的深入理解
2013/06/05 PHP
php使用cookie显示用户上次访问网站日期的方法
2015/01/26 PHP
php遍历CSV类实例
2015/04/14 PHP
PHP7正式版测试,性能惊艳!
2015/12/08 PHP
PHP递归删除多维数组中的某个值
2017/04/17 PHP
php读取XML的常见方法实例总结
2017/04/25 PHP
PHP聚合式迭代器接口IteratorAggregate用法分析
2017/12/28 PHP
Yii2.0框架模型添加/修改/删除数据操作示例
2019/07/18 PHP
tagName的使用,留一笔
2006/06/26 Javascript
javascript之水平横向滚动歌词同步的应用
2007/05/07 Javascript
最常用的12种设计模式小结
2011/08/09 Javascript
javascript写的简单的计算器,内容很多,方法实用,推荐
2011/12/29 Javascript
浅谈JSON和JSONP区别及jQuery的ajax jsonp的使用
2014/11/23 Javascript
js获取时间精确到秒(年月日)
2016/03/16 Javascript
jQuery Mobile 和 Kendo UI 的比较
2016/05/05 Javascript
jquery实现无刷新验证码的简单实例
2016/05/19 Javascript
JS采用绝对定位实现回到顶部效果完整实例
2016/06/20 Javascript
微信小程序 扎金花简单实例
2017/02/21 Javascript
react router 4.0以上的路由应用详解
2017/09/21 Javascript
Bootstrap 3多级下拉菜单实例
2017/11/23 Javascript
Webpack的dll功能使用
2018/06/28 Javascript
JavaScript原型对象、构造函数和实例对象功能与用法详解
2018/08/04 Javascript
微信小程序实现两个页面传值的方法分析
2018/12/11 Javascript
解决vue-cli项目开发运行时内存暴涨卡死电脑问题
2019/10/29 Javascript
详解pandas删除缺失数据(pd.dropna()方法)
2019/06/25 Python
详解python中的index函数用法
2019/08/06 Python
keras做CNN的训练误差loss的下降操作
2020/06/22 Python
获取python运行输出的数据并解析存为dataFrame实例
2020/07/07 Python
[原创]赚疯了!转手立赚800+?大佬的python「抢茅台脚本」使用教程
2021/01/12 Python
详解CSS3 filter:drop-shadow滤镜与box-shadow区别与应用
2020/08/24 HTML / CSS
实现向右循环移位
2014/07/31 面试题
开展党的群众路线教育实践活动个人对照检查材料
2014/11/05 职场文书
裁员通知
2015/04/25 职场文书
给校长的建议书范文
2015/09/14 职场文书
CocosCreator ScrollView优化系列之分帧加载
2021/04/14 Python
python中filter,map,reduce的作用
2022/06/10 Python