用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使用minidom读写xml的方法
Jun 03 Python
Python的Tornado框架实现图片上传及图片大小修改功能
Jun 30 Python
Python实现多线程抓取网页功能实例详解
Jun 08 Python
Python3.6简单操作Mysql数据库
Sep 12 Python
matplotlib设置legend图例代码示例
Dec 19 Python
解决在pycharm中显示额外的 figure 窗口问题
Jan 15 Python
pyqt5 实现多窗口跳转的方法
Jun 19 Python
对Django url的几种使用方式详解
Aug 06 Python
Python List列表对象内置方法实例详解
Oct 22 Python
Keras之fit_generator与train_on_batch用法
Jun 17 Python
通过实例简单了解python yield使用方法
Aug 06 Python
python中lower函数实现方法及用法讲解
Dec 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
防止MySQL注入或HTML表单滥用的PHP程序
2009/01/21 PHP
浅析php过滤html字符串,防止SQL注入的方法
2013/07/02 PHP
PHP Session 变量的使用方法详解与实例代码
2013/09/11 PHP
解决file_get_contents无法请求https连接的方法
2013/12/17 PHP
简单的php中文转拼音的实现代码
2014/02/11 PHP
php读取excel文件示例分享(更新修改excel)
2014/02/27 PHP
PHP实现更改hosts文件的方法示例
2017/08/08 PHP
php生成word并下载代码实例
2019/03/15 PHP
监控 url fragment变化的js代码
2010/04/19 Javascript
新鲜出炉的js tips提示效果
2011/04/03 Javascript
javaScript 利用闭包模拟对象的私有属性
2011/12/29 Javascript
原生JS实现表单checkbook获取已选择的值
2013/07/21 Javascript
javascript教程之不完整的继承(js原型链)
2014/01/13 Javascript
jquery常用操作小结
2014/07/21 Javascript
VC调用javascript的几种方法(推荐)
2016/08/09 Javascript
jQuery扇形定时器插件pietimer使用方法详解
2017/07/18 jQuery
简单了解JavaScript arguement原理及作用
2020/05/28 Javascript
[56:47]Ti4 循环赛第三日 iG vs Liquid
2014/07/12 DOTA
Python多线程编程(八):使用Event实现线程间通信
2015/04/05 Python
Python使用scrapy采集时伪装成HTTP/1.1的方法
2015/04/08 Python
在Python中使用SimpleParse模块进行解析的教程
2015/04/11 Python
python使用fcntl模块实现程序加锁功能示例
2017/06/23 Python
Python正则表达式和re库知识点总结
2019/02/11 Python
在Python中使用MySQL--PyMySQL的基本使用方法
2019/11/19 Python
详解python内置常用高阶函数(列出了5个常用的)
2020/02/21 Python
pycharm 关闭search everywhere的解决操作
2021/01/15 Python
经理秘书找工作求职信
2013/12/19 职场文书
小学生安全保证书
2014/02/01 职场文书
优秀团员自我评价范文
2014/04/23 职场文书
先进事迹材料范文
2014/12/29 职场文书
费用申请报告范文
2015/05/15 职场文书
2016年班主任新年寄语
2015/08/18 职场文书
详解Java实现设计模式之责任链模式
2021/06/23 Java/Android
Node与Python 双向通信的实现代码
2021/07/16 Javascript
Win10加载疑难解答时出错发生意外错误的解决方法
2022/07/07 数码科技
MySQL表字段数量限制及行大小限制详情
2022/07/23 MySQL