Python 判断图像是否读取成功的方法

yipeiwu_com6年前Python基础

大批量处理数据时,若因个别图像错误导致代码中断,从头再来比较浪费时间

对未成功读入的图像跳过(读图 import cv2)

for i in range(1,1000):
 image = cv2.imdecode(np.fromfile('xxx.jpg', dtype=np.uint8), -1)
 try:
  image.shape 
 except:
  print('fail to read xxx.jpg')
  continue
 ......

若该图像可能不存在,即没有该图像的文件名,也可用try判断

try:
 np.fromfile('xxx.jpg', dtype=np.uint8)
except:
 continue

以上这篇Python 判断图像是否读取成功的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python BlockingScheduler定时任务及其他方式的实现

本文介绍了python BlockingScheduler定时任务及其他方式的实现,具体如下: #BlockingScheduler定时任务 from apscheduler.sc...

Python中使用gzip模块压缩文件的简单教程

压缩数据创建gzip文件 先看一个略麻烦的做法   import StringIO,gzip content = 'Life is short.I use python'...

python训练数据时打乱训练数据与标签的两种方法小结

如下所示: <code class="language-python">import numpy as np data = np.array([[1,1],[2,2...

python多线程扫描端口(线程池)

扫描服务器ip开放端口,用线程池ThreadPoolExecutor,i7的cpu可以开到600个左右现成,大概20s左右扫描完65535个端口,根据电脑配置适当降低线程数 #!/u...

python调用c++ ctype list传数组或者返回数组的方法

示例1: pycallclass.cpp: #include <iostream> using namespace std; typedef unsigned char...